How to handle images in application...
Hi all,
I have a situation in my application where have buttons that each will have 3 images that represent it's state (normal, disabled, and pressed). What I want to know is how to store these images within the application? Should they be placed in an ImageList, or just referenced directly from a location? Or is there a better way?
Thanks
[346 byte] By [
Tryst] at [2007-12-23]
What I would do is to link the control with an imagelist that holds the images for the states. Say 1 ImageList per control or 1 ImageList for all control 'Up' images. Another Imagelist for all the control 'Down' images. The latter is how Delphi works - and it works well. Each control then has an ImageList property for Up, Down, etc images.
But then you load the ImageLists from Assembly resources whenever required. This allows you to apply various themes as well as support different resolution screens easily by simply changing the resource assembly that is loaded.
Create a new device project class library. This will result in a new DLL to be generated. This will be your resource assembly.
Remove the default Class1.cs
Add a bitmap to the project
Set the bitmap file Build Action property to Embedded Resource. The bitmap will now be embedded as a resource in the assembly (.dll). Deploy this dll with the application. In the same folder or any other. Easier to deploy into same folder.
To load the assembly:
Assembly MyResourceAssembly = Assembly.LoadFrom(@"fullpath\MyResourceAssembly.dll");
To list all embedded resources in assembly:
string[] s = MyResourceAssembly.GetManifestResourceNames();
To create / load a bitmap from assembly:
Stream BitmapStream = MyResourceAssembly.GetManifestResourceStream(AResourceName); // e.g. AssemblyNamespace.BitmapFilename
Bitmap MyBitmap = new Bitmap(BitmapStream); // You can do this in 1 line. I used 2 to show what has to happen.
Now you have a bitmap object loaded from your resource assembly.
The reason for having only bitmaps (or other resources) in the resource asembly is so that you can exchange them at will. You can also embed resources in the sam eassembly as your code if needed....