469,646 Members | 1,558 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

How load icon from embeded resource into imageList?

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


Nov 15 '05 #1
4 48209
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.GetExecutingAsse mbly().GetManifestResource
Stream("C_SharpPDA.DictateDis.jpg"));
case ImageName.RecordEnabled:

return new
Bitmap(System.Reflection.Assembly.GetExecutingAsse mbly().GetManifestResource
Stream("C_SharpPDA.dictate.jpg"));
case ImageName.Recording:

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

case ImageName.PlayEnabled:

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

case ImageName.PlayDisabled :

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

case ImageName.StopDisabled :

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

case ImageName.StopEnabled:

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

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

}

"Ed Sutton" <no****************@nomadics.com> wrote in message
news:ur**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
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
Nov 15 '05 #3
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
Nov 15 '05 #4
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
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Paul Cheetham | last post: by
1 post views Thread by =?Utf-8?B?QWRhbQ==?= | last post: by
reply views Thread by Pitaridis Aristotelis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.