Help checking BackgroundImage
I am using this code to check a backgroundimage:
if (actionbox1.BackgroundImage == "WaterforMGC.strollinstu.png")
But I get the error Operator '==' cannot be applied to operands of type 'System.Drawing.Image' and 'string'
So how can I开发者_StackOverflow中文版 check the BackgroundImage property?
Just in case, here's my randomizing code:
//actionbox1
var imageNames = new List<string> { "WaterforMGC.strollinstu.png", "WaterforMGC.blank.png", "WaterforMGC.swoopinstu.png", "WaterforMGC.waterbottle.png", "WaterforMGC.goop.png", "WaterforMGC.blank.png" };
var rand = new Random();
var index = rand.Next(0, imageNames.Count - 1);
var s = this.GetType().Assembly.GetManifestResourceStream(imageNames[index]);
actionbox1.BackgroundImage = Image.FromStream(s);
One solution could be simply first load, folowing your example, WaterforMGC.strollinstu.png
file into System.Drawing.Image
object, and after assign it to the actionbox1.BackgroundImage
.
At the moment when you wnat to figure out the exact image, should be enough to check equality between two objects (which actually will call GetHashCode()
)
Example:
//somewhere in the code
Image img1 = Image.FromFile(@".\.\....\\.\WaterforMGC.strollinstu1.png");
Image img2 = Image.FromFile(@".\.\....\\.\WaterforMGC.strollinstu2.png");
//assign to back image IMG1
actionbox1.BackgroundImage = img1;
//when comes moment to check whcih image is assigned (base on your app logic)
if(actionbox1.BackgroundImage == img1)
{
//do somethinmg here, based on your logic
}
else if(actionbox1.BackgroundImage == img2)
{
//do somethinmg other, based on your logic
}
Hope this helps.
Regards.
精彩评论