March 23, 2012
This article is not in the new development issues, but an old school issue.
I started an year ago to migrate most of my application from Windows Forms to WPF platform. it was good most of the images also
transferred to anew and sexy look.
BUT, than tow weeks ago I found old window that I needed to migrate and there was not problem with the images, but that in the past
we used the ImageList structure and as you know the image stored the image data imbedded in the Control Resx file
So I had two options :
- Mess my code and after the “InitializeComponent()” run over the images and save them. No Way !!!
- Create external tool to extract those images for me.
This nice thing is that my creating this tool I remembered that there are images that also imbedded in the project but not as part of the ImageList
So, there for you an easy way to get your resources.
It’s Quite simple we load the Resx file to ResXResourceReader enumerate it and extract the images.
1: ResXResourceReader reader = new ResXResourceReader(currentResxFile);
2: var enumerator = reader.GetEnumerator();
3: while (enumerator.MoveNext())
Still to extract the data from ImageList we’ll need to create an instant of it, populate the that (stream data) into the ImageStream after it run over
the imbedded images.
1: ImageListStreamer ils = value as ImageListStreamer;
2: if (ils != null)
4: ImageList imageList1 = new ImageList();
5: imageList1.ImageStream = ils;
6: if (imageList1 != null && imageList1.Images != null && imageList1.Images.Count > 0)
8: for (int i = 0; i < imageList1.Images.Count; i++, imageIndex++)
To save the images we’ll use the build-in function Image.Save to do so
We only need to decide the image extension, for that we use the Image.RawFormat
1: private static string GetExtension(ImageFormat imageFormat)
3: if (ImageFormat.Bmp.Guid == imageFormat.Guid)
4: return ".bmp";
5: if (ImageFormat.Emf.Guid == imageFormat.Guid)
6: return ".emf";
7: if (ImageFormat.Exif.Guid == imageFormat.Guid)
8: return ".exif";
9: if (ImageFormat.Gif.Guid == imageFormat.Guid)
10: return ".gif";
11: if (ImageFormat.Icon.Guid == imageFormat.Guid)
12: return ".ico";
13: if (ImageFormat.Jpeg.Guid == imageFormat.Guid)
14: return ".jpeg";
15: if (ImageFormat.MemoryBmp.Guid == imageFormat.Guid)
16: return ".bmp";
17: if (ImageFormat.Png.Guid == imageFormat.Guid)
18: return ".png";
19: if (ImageFormat.Tiff.Guid == imageFormat.Guid)
20: return ".tiff";
21: if (ImageFormat.Wmf.Guid == imageFormat.Guid)
22: return ".wmf";
23: return ".bmp";
So, hope it’s helped you
You can download the source code from here.
Or Executable from here.