2011-07-14 164 views
0

我使用此代码来检查背景图片:帮助检查的BackgroundImage

if (actionbox1.BackgroundImage == "WaterforMGC.strollinstu.png") 

但我得到的错误操作符“==”不能应用于类型“为System.Drawing.Image”和“字符串操作数'

那么如何检查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

您无法将字符串与图像进行比较。您必须保存已加载的图像的路径,以便比较两个字符串,或者首先加载路径指向的图像,以便比较两个图像。 –

回答

0

一种解决方案可能是简单的第一负载,如下因素的例子,WaterforMGC.strollinstu.png 文件到System.Drawing.Image对象,之后将其分配给actionbox1.BackgroundImage

在当你wnat找出确切的图像的那一刻,应该是足够的检查

例两个物体之间的平等(这实际上将调用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 
    } 

希望这有助于。

问候。

+0

嗯...请详细解释。我是一个n00b – janj

+0

我更新了代码,看看。 – Tigran

+0

谢谢!我会看看我能做什么。 – janj