C# 将datagridview单元格值与DataGridViewImageColumn中的图像进行比较

C# 将datagridview单元格值与DataGridViewImageColumn中的图像进行比较,c#,datagridview,compare,datagridviewcolumn,C#,Datagridview,Compare,Datagridviewcolumn,我已经创建了一个DataGridViewImageColumn,如果图像单元格的值为绿色,则希望执行该操作。 代码如下所示。但它并没有进入状态 if (dgvException.Rows[e.RowIndex].Cells["colStock"].Value == Properties.Resources.msQuestion) { //Some code } 请提供帮助。使用相等运算符(==

我已经创建了一个
DataGridViewImageColumn
,如果图像单元格的值为绿色,则希望执行该操作。 代码如下所示。但它并没有进入状态

if (dgvException.Rows[e.RowIndex].Cells["colStock"].Value 
                                              == Properties.Resources.msQuestion)
{
    //Some code
}

请提供帮助。

使用相等运算符(
==
)检查图像的相等性无法按您需要的方式工作。这就是为什么等式检查总是返回false

您需要找出两个图像的内容是否相同——为此,您需要对DGV单元中的图像和参考图像进行逐像素检查。我找到了一些链接,演示了如何比较两幅图像。我采用了文章中的图像比较算法,并将其压缩为一种方法,该方法将两个
位图作为参数进行比较,如果图像相同,则返回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;
    }
}

使用相等运算符(
==
)检查图像的相等性并不能按您需要的方式工作。这就是为什么等式检查总是返回false

您需要找出两个图像的内容是否相同——为此,您需要对DGV单元中的图像和参考图像进行逐像素检查。我找到了一些链接,演示了如何比较两幅图像。我采用了文章中的图像比较算法,并将其压缩为一种方法,该方法将两个
位图作为参数进行比较,如果图像相同,则返回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;
    }
}

我建议使用cell Tag属性添加一个表示图像的文本值,例如一个数字或名称,并使用它检查显示的图像

我建议使用cell-Tag属性添加一个表示图像的文本值,例如一个数字或名称,并使用它检查显示的图像

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;                        
}                         

在我的案例中,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;                        
}                         

非常感谢杰伊·里格斯。代码工作起来很有魅力。真的很感谢你的帮助。对于一张小图片来说很好,但是对于大图片呢?非常感谢@Jay Riggs。代码工作起来很有魅力。真的很感谢你的帮助。对于一个小的图像是好的,但是对于大的图像呢?