【问题标题】:How Can I compare (pictureBox1.Image with Properties.Resources.bug1)? [duplicate]如何比较(pictureBox1.Image 与 Properties.Resources.bug1)? [复制]
【发布时间】:2018-09-18 21:45:08
【问题描述】:

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

我读到我不能以那种形式做到这一点:

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

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

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

【问题讨论】:

    标签: c# equals picturebox


    【解决方案1】:

    我的工作假设是,在您的代码中的某个时刻,您正在执行类似

    的操作
    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));
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 2017-12-11
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多