How load icon from embeded resource into imageList?

E

Ed Sutton

I have been searching for how to get an icon resource and load it into an
ImageList. I added an *.ico file to my project and set the files "Build
Action" to "Embedded Resource". Is there any easy way to do this? This was
easy to do in the old MFC. I can not find the answer in C#.

I also need to embed an XML file into the resource and load it at run time.
I am hoping once I figure out how to load an icon resource then loading an
XML file will be similar.

Thanks in advance for any tips or suggestions,

-Ed
 
W

William Ryan

This is a call to get the bitmap. the funciton is below:



picStop.Image = GetImage(ImageName.StopDisabled);



private Bitmap GetImage(ImageName i)

{

switch(i){

case ImageName.RecordDisabled:

return new
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResource
Stream("C_SharpPDA.DictateDis.jpg"));


case ImageName.RecordEnabled:

return new
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResource
Stream("C_SharpPDA.dictate.jpg"));


case ImageName.Recording:

return new
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResource
Stream("C_SharpPDA.dictate.jpg"));

case ImageName.PlayEnabled:

return new
Bitmap(System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceSt
ream("C_SharpPDA.play.jpg"));

case ImageName.PlayDisabled :

return new
Bitmap(System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceSt
ream("C_SharpPDA.playinverted.jpg"));

case ImageName.StopDisabled :

return new
Bitmap(System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceSt
ream("C_SharpPDA.stopdisabled.jpg"));

case ImageName.StopEnabled:

return new
Bitmap(System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceSt
ream("C_SharpPDA.stop.jpg"));



}


return new
Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResource
Stream("C_SharpPDA.dictate.jpg"));

}
 
E

Ed Sutton

Thank you for your reply. I still do not understand. .
Button09.Image = (Image) new Bitmap (this.GetType
(),"Resources.Images.Image1.ico");

All images are in the ../Resources/Images directory.
Take attention for the namespace. It must be the same.

Here's what I am hearing:

Step 1 - Manually create "/Resources/Images" subfolder underneath my C#
project directory and copy icon files into it

Step 2- Add all the files in the Images subfolder to my C# project. Set the
"Build Action" property for each image file to "Embedded Resource".

Step 3 - ???

It looks like you somehow created namespaces to match your
/Resources/Images" directory structure. How are the namespaces created for
your resources?

Thanks in advance,

-Ed
 
E

Ed Sutton

Also, how is the "Resource View" used in VStudio.NET?

In my C# project it is empty. It is not intuitive to me how to add
resources to it.

Thanks again,

-Ed
 
E

Ed Sutton

For future Google'ers here is my summary:

How to load icon from embedded resource into imageList
-------------------------------

1 - Add your *.ico file to your solution project. Pay attention to filename
case sensitivity. This is important.
2 - Set the "Build Action" property for this file to "Embedded Resource"
3 - Load the icons by using the Bitmap constructor that loads fro the
resources and cast to an Image.

// Load the images in an ImageList.
ImageList myImageList = new ImageList();
myImageList.Images.Add((Image)new Bitmap(GetType(), "Reader.ico"));
myImageList.Images.Add((Image)new Bitmap(GetType(), "Location.ico"));
myImageList.Images.Add((Image)new Bitmap(GetType(), "Logger.ico"));


In example above, if you did not pay attention to file name case
sensitivity, and you typed the following.

myImageList.Images.Add((Image)new Bitmap(GetType(), "READER.ICO"));

You would get an exception stating the resource could not be found in your
class.

-Ed
 

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