Checking equality for images using the equality operator (==
) doesn't work the way you need it to work. That's why with your equality check always returns false.
You need to find out if the content of two images are the same -- to do this you'll need to do a pixel by pixel check of the image in the DGV cell and the reference image. I found a few links to this article that demonstrates comparing two images. I've taken the image comparison algorithm from the article and condensed it into a method that takes two Bitmaps
to compare as parameters and returns true if the images are identical:
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;
}
}
(warning: code not tested)
Using this method your code becomes:
if (CompareImages((Bitmap)dgvException.Rows[e.RowIndex].Cells["colStock"].Value, Properties.Resources.msQuestion)) {
//Some code
}