0

How I Can tell if that image(pictureBox1.Image) is the same as image from Properties.Resources.bug1 ???

I read that i cant do it in that form :

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

I found this "You need your own Image comparison algorithm if you need to compare it. You'd do it by comparing pixel by pixel."

so what that mean, How I can do it properly??

1 Answers1

0

I'm working under the assumption that, at some point in your code, you are doing something like

pictureBox1.Image = Properties.Resources.bug1;

If you're getting the image from elsewhere and it differs in some small way, this method won't work. I'm sure there are better, more efficient ways of doing this, but here's something:

  1. Convert image1 to a byte array.
  2. Convert image2 to a byte array.
  3. Compare these arrays to see if they're the same.

    private byte[] GetImageBytes(Image img)
    {
        using (var ms = new System.IO.MemoryStream())
        {
            ImageConverter imgConverter = new ImageConverter();
            return (byte[])imgConverter.ConvertTo(img, typeof(byte[]));
        }
    }
    

    Implementation:

    bool sameImage = GetImageBytes(pictureBox1.Image).SequenceEqual(GetImageBytes(Properties.Resources.bug1));
    
FvL
  • 36
  • 1
  • 4