How to retrive an image from a resource file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, i have a resourcefile with my project that contains a jepg image. Now i
want to retrive that image to a Image m_Image in my program. Whats the best
way to do this?
 
Patrick F said:
Hi, i have a resourcefile with my project that contains a jepg image. Now i
want to retrive that image to a Image m_Image in my program. Whats the best
way to do this?

Assembly a = Assembly.GetExecutingAssembly();
Stream stream = a.GetManifestResourceStream( "MyNamespace.MyImage.jpg" );
if (stream != null)
{
this.m_Image = Bitmap.FromStream( stream ) as Bitmap;
stream.Close();
}

You have to ensure MyImage.jpg is included in your project and marked as an
"Embedded Resource" in its properties. In the example above, MyImage.jpg
must be located in the root project folder. If it's located in a subfolder,
such as "Art", you would need to add that to the path, using a period instead
of backslash, such as:

"MyNamespace.Art.MyImage.jpg"

If the image is not loading, you can confirm its existence and path in your
resource file by adding the following line to the code above:

// use this to get the names of all resources in your assembly
string [] resNames = a.GetManifestResourceNames();
 
thanks alot, works now

Mini-Tools Timm said:
Patrick F said:
Hi, i have a resourcefile with my project that contains a jepg image. Now i
want to retrive that image to a Image m_Image in my program. Whats the best
way to do this?

Assembly a = Assembly.GetExecutingAssembly();
Stream stream = a.GetManifestResourceStream( "MyNamespace.MyImage.jpg" );
if (stream != null)
{
this.m_Image = Bitmap.FromStream( stream ) as Bitmap;
stream.Close();
}

You have to ensure MyImage.jpg is included in your project and marked as an
"Embedded Resource" in its properties. In the example above, MyImage.jpg
must be located in the root project folder. If it's located in a subfolder,
such as "Art", you would need to add that to the path, using a period instead
of backslash, such as:

"MyNamespace.Art.MyImage.jpg"

If the image is not loading, you can confirm its existence and path in your
resource file by adding the following line to the code above:

// use this to get the names of all resources in your assembly
string [] resNames = a.GetManifestResourceNames();

--
Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top