Here's my code:
borderTop.BackgroundImage == Properties.Resources.buttonTopSelected
I need to see if the .BackgroundImage property is the same as the picture I added to resource. How may I compare t开发者_如何学Chese two images?
In code, one is a Image and one is a Bitmap.
Any suggestions?
You need to store the references so you can compare them later. Something like:
Bitmap top = Properties.Resources.buttonTopSelected;
Bitmap bottom = Properties.Resources.buttonBottomSelected;
...
borderTop.BackgroundImage = top;
...
if (borderTop.BackgroundImage == top) {
// etc..
}
Don't forget to dispose them in the form's Dispose() method.
The comparison won't work as they are not the same object, this is comparing references not object properties. While you could compare the images pixel by pixel that's somewhat overkill, I'd probably suggest using some other way to store the button state either in an existing button property or by extending the button into your own class that has additional information (using composition or inheritance).
----- EDIT -----
You could add state to the Button through inheritance, i.e.:
public class MyButtonWithState : Button
{
public int ButtonState { get; set; }
}
You can then instantiate your buttons using MyButtonWithState rather than just button.
public Form1()
{
InitializeComponent();
MyButtonWithState NewButton = new MyButtonWithState();
NewButton.Text = "My Test Button";
NewButton.ButtonState = 3;
this.Controls.Add(NewButton);
}
If you want this available at design time there's a bit more to do but this should give you the basic idea.
精彩评论