How To Load Bitmaps

S

Saurabh

Hi All,

I have a C# project, I added 2 bitmap images, 1.bmp and 2.bmp in the project
by right clicking and using the add existing item dialog. The bmp files now
appear in the solution explorer. My question is, do they get embedded in the
assembly? When I use the code as

System.Drawing.Bitmap b =
(System.Drawing.Bitmap)Bitmap.FromFile("c:\\myimages\\1.bmp");

it loads the bitmap and works fine. There are 2 things, I do not want to
hard code the path as somebody else might have the project somewhere else.
Ideally I would want to load the image from the assembly, but when I do

ResourceManager rm;
rm = new ResourceManager("<MyAssemblyName>",
Assembly.GetExecutingAssembly());
System.Drawing.Bitmap bt;
bt = (System.Drawing.Bitmap)rm.GetObject("1.bmp");

it does not load the bitmap. I think I am missing something very basic over
here. Can anybody point to what might be wrong?

TIA,

--Saurabh
 
C

ClayB [Syncfusion]

When you are adding the bitmap file to your project, are you setting its
Build Action to embedded resource?

George Shepherd's Windows Forms FAQ contains an entry entitled:

How do I load a BMP file that has been added to my solution as an
embedded resource?

Check it out at:
http://www.syncfusion.com/faq/winforms/search/649.asp

=============================================
Clay Burch, .NET MVP
Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials
 
G

Glen Jones MCSD

Saurabh,

No, adding the bitmaps to the solution will have their compile type set to
"content".

You will need to load the bitmaps into a resourse file, then add the
resourse file to a project to include them in the compiled assembly.

I never did figure out using the VS.Net ID to create a Resource file. So I
wrote my own Resourse Editor.
One hint I can give you is you will need to use a XMLResource file, if you
want to use in in a WinForm easily.

Objects to look into:

ResXResourceReader
ResXResourceWriter

Hope this helps.
 
S

Saurabh

It seems that having the type set to "content" was a problem for me, and I
am happy that at least i was thinking on the correct lines that the bmps are
not getting added to the assembly.

Thanks for your inputs.

I also had faced difficulty in working out how to get resource editor in VS
IDE but if u right click on a project in solution explorer and do add new
item and select the item as "Assembly Resource File", it opens the XML
resource editor (or something like that) where I could add string resources
but for bitmaps I explicitely added the bitmap files.

Thanks once again for your help.

--Saurabh
 

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