2012-10-03 98 views

回答

1

使用相等運算符(==)檢查圖像的相等性不符合您的工作需要。這就是爲什麼你的平等檢查總是返回false。

您需要確定兩個圖像的內容是否相同 - 要做到這一點,您需要逐個像素檢查DGV單元中的圖像和參考圖像。我找到幾個指向this article的鏈接來演示比較兩張圖片。我已經從物品拍攝的圖像比較算法和其冷凝成帶有兩個Bitmaps比較作爲參數,並且如果圖像是相同的,則返回true的方法:

private static bool CompareImages(Bitmap image1, Bitmap image2) { 
    if (image1.Width == image2.Width && image1.Height == image2.Height) { 
     for (int i = 0; i < image1.Width; i++) { 
      for (int j = 0; j < image1.Height; j++) { 
       if (image1.GetPixel(i, j) != image2.GetPixel(i, j)) { 
        return false; 
       } 
      } 
     } 
     return true; 
    } else { 
     return false; 
    } 
} 

(警告:代碼未測試)

使用這種方法你的代碼變成:

if (CompareImages((Bitmap)dgvException.Rows[e.RowIndex].Cells["colStock"].Value, Properties.Resources.msQuestion)) { 
    //Some code 
} 
+0

非常感謝你@Jay Riggs。代碼像魅力一樣工作。非常感謝您的幫助。 –

+0

對於小圖像來說很好,但大圖像呢? – MatanKri

1

我會建議使用細胞標記屬性添加表示圖像的文本價值 - 例如,一個電話號碼或姓名,並用它來檢查顯示的內容圖像。 。

0

S.Ponsford答案在我的案例中工作得很好,我做了這個通用的例子。 PD:請記住我的專欄0是我的DataGridViewImageColumn

if (this.dataGridView.CurrentRow.Cells[0].Tag == null) 
{              
    this.dataGridView.CurrentRow.Cells[0].Value= Resource.MyResource1; 
    this.dataGridView.CurrentRow.Cells[0].Tag = true;      
} 
else 
{ 
    this.dataGridView.CurrentRow.Cells[0].Value = Resources.MyResource2; 
    this.dataGridView.CurrentRow.Cells[0].Tag = null;       
}       
相關問題