read images from an ImageListStreamer

M

mikemeamail

Hi all,

Here's a simple question, how can i read the images from an
ImageListStreamer object in the same way i read a resource file
(.resources) containing images ?

For example, assuming i have a .resources file which contains some
bitmaps and icons. I can easily parse this file using a ResourceReader
object and enumerate each resource and cast it to its appropriate type,
wether Bitmap or Icon.

Now, what if those images would be embedded in an imagelist on a form?
I saw that if i access the resource, from the .resx file of the form,
the imagelist is of type ImageListStreamer but how can i read and
enumerate it then ?

Any help appreciated,
Thanks
 
M

mikemeamail

Ok after having searched myself i could find the solution. It's quite
easy in fact,
when reading the resource file, the resources from an imagelist are
from ImageListStreamer type. We need then to use an ImageList object
and assign the ImageListStreamer to it. Then it's quite easy to read
the ImageList object as shown in this piece of code :


//get first the assemply
Assembly a = Assembly.LoadFrom("resourcefilepath");

//define a type for check purpose
Type listStreamer = typeof(System.Windows.Forms.ImageListStreamer);

//get all the resources name in this assembly
string []names = a.GetManifestResourceNames();
if (names.Length > 0)
{
//Get each resources from this assembly and read it
int namesCount = names.Length;
for (int namesCounter = 0; namesCounter < names.Length; namesCounter++)

{
System.IO.Stream s = a.GetManifestResourceStream(names[namesCounter]);
if (s != null)
{
IResourceReader reader = new ResourceReader(s);
IDictionaryEnumerator en = reader.GetEnumerator();
while (en.MoveNext())
{

if(en.GetType() == listStreamer)
{
ImageListStreamer s = (ImageListStreamer)en.Value;
ImageList imgList = new ImageList();

imgList.ImageStream = s;
//iterate then through the imagelist object
foreach(Image img in imgList.Images)
{
FileStream f = new FileStream("path\filename",FileMode.Create);
imag.Save(f,ImageFormat.bmp);
f.close;
}
}

}
}
}
}
 

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