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.?
-
1An 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.– Hans PassantFeb 8, 2012 at 13:49
-
even i know the resource name how do i compare that with the button background image name?– Arunkumar ChandrasekaranFeb 8, 2012 at 14:18
-
1You 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.– Hans PassantFeb 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.– Andrew BarberFeb 8, 2012 at 14:38
2 Answers
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.
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.