Loading an image form an assembly

R

Roy Chastain

I have an application that needs to have a .gif or a .bmp located within it (resource type thing for unmanaged code). I would like to be
able to retrieve that image into an Image object within the running program.

How do I add an image to an assembly created with C#?

How do I go about loading that image into an Image object at run time.

Thanks
 
G

Guest

Hi.
I suppose you can create a resource file using Resgen.exe and then add the image file from there. Then you can from VS.NET create an empty project and add the resource file you have created (set output to classs library) and build it. It will generate a DLL with your image
Use the class System.Reflection.Assembly to load your DLL at run time. Asociate an instance of ResourceManager with the specific resource you r interested, in this case the resorce with your image. Then use the method ResourceManager.GetObject and use a typecast to convert the oject to an image object. Thats it

regards, ale
 
G

Guest

You can embed the image in the assembly, rather than in a resource file:

1) Add the image to your project (Image.bmp below)
2) Go to the image properties and set the Build Action to 'Embedded Resource'
3) When you want to obtain the image again, use the following code:

string resource = string.Format( "{0}.Image.bmp", this.GetType( ).Namespace );

System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly( ).GetManifestResourceStream( resource );

System.Drawing.Image image = new System.Drawing.Bitmap( stream );

stream.Close( );

HTH

RS
 
R

Roy Chastain

Thanks
Looks like just what I needed.

You can embed the image in the assembly, rather than in a resource file:

1) Add the image to your project (Image.bmp below)
2) Go to the image properties and set the Build Action to 'Embedded Resource'
3) When you want to obtain the image again, use the following code:

string resource = string.Format( "{0}.Image.bmp", this.GetType( ).Namespace );

System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly( ).GetManifestResourceStream( resource );

System.Drawing.Image image = new System.Drawing.Bitmap( stream );

stream.Close( );

HTH

RS
 
H

Herfried K. Wagner [MVP]

* Roy Chastain said:
I have an application that needs to have a .gif or a .bmp located
within it (resource type thing for unmanaged code).

Are you talking about Win32 resources?
 

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

Top