帮助检查背景图像
本文关键字:图像 背景 检查 帮助 | 更新日期: 2023-09-27 18:02:19
我使用这个代码来检查背景图像:
if (actionbox1.BackgroundImage == "WaterforMGC.strollinstu.png")
但是我得到错误操作符'=='不能应用于'System.Drawing '类型的操作数。图像'和'字符串'
我如何检查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);
一个解决方案可以简单地按照您的示例首先加载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
}
希望对你有帮助。
问候。