我正在使用此代码检查backgroundimage:
if (actionbox1.BackgroundImage == "WaterforMGC.strollinstu.png")
但是我收到错误操作符'=='不能应用于'System.Drawing.Image'和'string'类型的操作数
那么如何查看BackgroundImage属性?
以防万一,这是我的随机代码:
//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);
答案 0 :(得分:1)
一个解决方案可能只是首先加载,在您的示例中,WaterforMGC.strollinstu.png
将文件分配到System.Drawing.Image
对象,然后将其分配给actionbox1.BackgroundImage
。
当你想知道确切的图像时,应该足以检查两个对象之间的相等性(实际上会调用GetHashCode()
)
示例:
//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
}
希望这有帮助。
问候。