
Introduction
Sometimes you want to extract an image from an existing .NET DLL which is not included as a default resource. For instance, you added an image to an ImageList
and forgot where the image came from. You now wish to save this image, which is impossible using the IDE of Visual Studio. To do this, I wrote this small piece of code, which is not completely foolproof, but should work in most occasions.
How it's done
First, I open the DLL and scan it for objects of type Form
(exception handling removed):
Assembly ass = Assembly.LoadFrom(textBox1.Text);
Type[] types = ass.GetExportedTypes();
foreach(Type type in types)
{
if (!type.IsAbstract && type .IsSubclassOf(typeof(Form)))
{
Form fm = (Form) ass.CreateInstance(type.FullName);
extractImages(fm);
}
}
Extracting the images is quite easy; just remember that there can be more than one ImageList
. The same can be done to extract images from other types of controls. I convert the images to PNG to recreate the transparency.
Type t = form.GetType();
FieldInfo field = t.GetField("components",
BindingFlags.NonPublic | BindingFlags.Instance );
IContainer comps = (IContainer)field.GetValue(form);
int imglist_idx=0;
foreach(Component c in comps.Components)
{
if (c is ImageList)
{
.... and so on, hope you get the idea.
I am a .NET developer from Gouda, the Netherlands, developing software since mid eighties.