我如何比较(pictureBox1.Image与Properties.Resources.bug1)? [重复]

问题描述 投票:0回答:1

我如何判断该图像(pictureBox1.Image)是否与Properties.Resources.bug1中的图像相同?

我读到我不能以这种形式这样做:

if (pictureBox1.Image == Properties.Resources.bug1)
 {
    MessageBox.Show("here");
 }

我发现这个“你需要自己的图像比较算法,如果你需要比较它。你可以通过逐像素比较来做到这一点。”

那是什么意思,我怎么能正确地做到这一点?

c# equals picturebox
1个回答
0
投票

我的工作假设是,在你的代码中的某些时候,你正在做类似的事情

pictureBox1.Image = Properties.Resources.bug1;

如果您从其他地方获取图像并且它在某种程度上有所不同,则此方法将不起作用。我确信有更好,更有效的方法,但这里有一些东西:

  1. 将image1转换为字节数组。
  2. 将image2转换为字节数组。
  3. 比较这些数组以查看它们是否相同。 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));
© www.soinside.com 2019 - 2024. All rights reserved.