Arunkumar Chandrasekaran
Arunkumar Chandrasekaran

Reputation: 1241

how to get the name of button background Image in Winforms C#

I added image to the resource in Form and set it as background Image for a button. How to get the name of the button's background image by through programmatic.?

Upvotes: 8

Views: 6237

Answers (2)

ken2k
ken2k

Reputation: 49013

If you have set the BackgroundImage property using the Winforms designer, Visual Studio has generated in the .Designer.cs file the following line for you:

this.button1.BackgroundImage = global::WindowsFormsApplication5.Properties.Resources.my_image_name;

As you can see, it's affecting a value of type System.Drawing.Bitmap to a property of type System.Drawing.Image. The only information you have here is the image itself.

If you added the image by yourself:

string resourceKey = "my_image_name";
this.button1.BackgroundImage = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject(resourceKey);

then you already have the name of your resource...

If it doesn't answer your question, then please make it clearer.

Upvotes: 0

Emond
Emond

Reputation: 50682

You cannot retrieve the name of the image.

What you can do is when you set the image: store the name of the image in the Tag property of the Button.

You can then check the Tag property as long as you keep it in sync with the image.

Even nicer would be to sub-class the Button and add a property that stores the name.

Upvotes: 4

Related Questions