Question

How can I use, if possible,

Activator.CreateInstance("MyExeName", "Resources.Image1") 

to create a background image that will be associated with a button? Image1 is the image defined in the resources named "Image1".

var handle = Activator.CreateInstance("TestApp", "Resources.Image1");
var backgroundImage = handle.Unwrap() as System.Drawing.Image;

I did not see anything like this in stackoverflow. What am I missing?

Thanks for looking and any help is appreciated.

Was it helpful?

Solution

If you can derive the name of the resource you want, then use this:

using System.Resources;

public static Image GetImage(String ImageName)
{
    Image retImage = null;
    Object o = Properties.Resources.ResourceManager.GetObject(ImageName);
    if (o != null && (o is Image))
    {
        Image img = (Image)((Image)o).Clone();  //necessary to prevent premature disposal
        retImage = img;
    }
    return retImage;
}

Here's the MSDN on ResourceManager, and there are many SO questions relating to its use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top