2011-07-14 166 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