Mr Awesome8
Mr Awesome8

Reputation: 281

Get a resource item by string winforms

In a C# Winforms app, how can I access Resources via string? For example I know I can do Properties.Resources.MyImage however, I won't know which image I need until runtime. With a string containing "MyImage", how can I access Properties.Resources.MyImage? Ideally, I was hoping for something easy such as Properties.Resources["MyImage"], but with some searching around haven't been able to find a quick solution to this.

Any help is appreciated.

Upvotes: 2

Views: 5418

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39152

You can use ResourceManager.GetObject() like this:

string resourceName = "MyImageNameHere";
Bitmap bmp = (Bitmap)Properties.Resources.ResourceManager.GetObject(resourceName);
pictureBox1.Image = bmp;

Upvotes: 6

Related Questions