2014-01-15 79 views
0

我有這樣更改默認圖像從資源

if (image == "") { 
    pictureBoxImage.Image = Properties.Resources.noimage; 
} 
else{ 
    pictureBoxImage.ImageLocation = Path.Combine(Global.myPictureLocation, image); 
    pictureBoxImage.BringToFront(); 
} 

,直到我從資源文件夾設置默認的形象,工作正常,編輯表單代碼。如果我從資源文件夾設置的圖像,而不是瀏覽圖片的代碼去錯誤.... 我嘗試MODIF像這樣

if (image == Properties.Resources.noimage) { 
    pictureBoxImage.Image = Properties.Resources.noimage; 
} 
else{ 
    pictureBoxImage.ImageLocation = Path.Combine(Global.myPictureLocation, image); 
    pictureBoxImage.BringToFront(); 
} 

,但它會與錯誤...

操作「 ==」不能應用於類型的操作數‘串’和‘System.Drawing.Bitmap’

如何正確的方式來檢測,如果它的形象從資源或不?

回答

0

image是一個字符串,你不能將它與圖像文件進行比較。

你可以嘗試像

if (Image.FromFile(image) == Properties.Resources.noimage)) 

或更好,但

if (Image.Equals(Image.FromFile(image), Properties.Resources.noimage)) 
+0

ü可以給一個想法怎麼做合適? – Neversaysblack

+0

@ user3184196你可以比較圖像文件本身,或使用圖像標籤 – 2014-01-15 06:25:50

+0

仍然混淆....也許你可以給我例:) – Neversaysblack