2

So I have a picturebox and I need to check what image it's currently displaying so I can put it in an if statement. Basically "if pictureBox1 image is diamond then do". All the images that I use in pictureboxes are in Resources. I tried something like if(pictureBox1.Image == Properties.Resources.diamond){} but that doesn't seem to work.

andrE
  • 25
  • 4
  • You may need to compare the content of byte arrays that define the data image (colors of pixels). –  Jul 04 '20 at 01:06
  • 1
    How do you set the image in the picture box? – Chetan Jul 04 '20 at 01:08
  • 1
    you dont, you store the information when you put it in – TheGeneral Jul 04 '20 at 01:09
  • @ChetanRanpariya pictureBox1.Image = Properties.Resources.diamond; – andrE Jul 04 '20 at 01:11
  • Once you got two byte arrays of images (https://stackoverflow.com/questions/3801275/how-to-convert-image-to-byte-array & http://net-informations.com/q/faq/imgtobyte.html), you can compare byte-per-byte. –  Jul 04 '20 at 01:12
  • Are you changing the image to some thing else in the code? Why you need to check what image is assigned to picture box? – Chetan Jul 04 '20 at 01:14

1 Answers1

2

Using How to convert image to byte array :

var array1 = ImageToByteArray(pictureBox1.Image);
var array2 = ImageToByteArray(Properties.Resources.diamond);

bool isSame = array1.Length == array2.Length;

if ( isSame )
  for ( int index = 0; index < array1.Length; index++)
    if ( array1[index] != array2[index] )
    {
      isSame = false;
      break;
    }

if ( isSame )
{
  ...
}

Also : How to compare two images?