这个问题在这里已有答案:
我如何判断该图像(pictureBox1.Image)是否与Properties.Resources.bug1中的图像相同?
我读到我不能以这种形式这样做:
if (pictureBox1.Image == Properties.Resources.bug1)
{
MessageBox.Show("here");
}
我发现这个“你需要自己的图像比较算法,如果你需要比较它。你可以通过逐像素比较来做到这一点。”
那是什么意思,我怎么能正确地做到这一点?
我的工作假设是,在你的代码中的某些时候,你正在做类似的事情
pictureBox1.Image = Properties.Resources.bug1;
如果您从其他地方获取图像并且它在某种程度上有所不同,则此方法将不起作用。我确信有更好,更有效的方法,但这里有一些东西:
private byte[] GetImageBytes(Image img)
{
using (var ms = new System.IO.MemoryStream())
{
ImageConverter imgConverter = new ImageConverter();
return (byte[])imgConverter.ConvertTo(img, typeof(byte[]));
}
}
执行:
bool sameImage = GetImageBytes(pictureBox1.Image).SequenceEqual(GetImageBytes(Properties.Resources.bug1));