8

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.?

4
  • 1
    An image doesn't have a name. You should have no problem knowing the original file name or resource name, after all you added it yourself. Feb 8, 2012 at 13:49
  • even i know the resource name how do i compare that with the button background image name? Feb 8, 2012 at 14:18
  • 1
    You don't, the background image doesn't have a name. You'll need to explain what you are really trying to do, it is completely unguessable. Feb 8, 2012 at 14:24
  • @arunkumarnonascii You are confusing knowing the name of the original file with the "name" of the Image object that is assigned to your control. The latter does not exist; it's simply an image object in memory. Feb 8, 2012 at 14:38

2 Answers 2

4

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.