Retrieving an Image from a resource file

D

Doug Handler

I'm using VS2k5 and i've added a bunch of images to the project's
resource.resx file and thus they reside in the Resources folder. I want to
dynamically add an image to my project. I have no problem adding an image
if its part of an existing UserControl or Form, but just as part of the
general project, I can't seem to figure out how to obtain the info via the
ComponentResourceManager.

Thanks in advance.

dh
 
F

Fabio Cannizzo

Find the correct resource imageName looking at your assembly Manifest (use
ildasm), then try this...

public Bitmap getBitmap( string imageName, t
AnyTypeWithinTheSameAssemblyNamespace ) {

Assembly myAssembly = Assembly.GetAssembly(t);

Stream myStream = myAssembly.GetManifestResourceStream( t, imageName );

Bitmap b = new Bitmap(myStream, true);

myStream.Close();

// Get the current desktop screen graphics.

Graphics g = Graphics.FromHwnd(IntPtr.Zero);

// Make a bitmap using that color depth.

Bitmap bitmap = new Bitmap(b.Width, b.Height, g);

// Clean up.

g.Dispose();


// Start drawing on our new bitmap.

g = Graphics.FromImage(bitmap);

// Copy the loaded bitmap to the bitmap with proper screen depth.

g.DrawImage(b, 0, 0, bitmap.Width, bitmap.Height);

// Clean up.

g.Dispose();

b.Dispose();


return bitmap;

}
 

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