1

How can we know if a specific image from a path is in a picture box?

I tried this but it doesn't work:

if (pictureBox.Image == Image.FromFile(@"..\..\resources\cirlce.png")) grilleArray[0] = 2;
else if (pictureBox.Image == Image.FromFile(@"..\..\resources\cross.png")) grilleArray[0] = 1;
else grilleArray[0] = 0;

So I want to know if the image in the picture box is the image that is in circle.png

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Why would you want to know this? Sounds like an XY Problem. – Sweeper Nov 01 '19 at 12:57
  • Possible duplicate of [Algorithm to compare two images in C#](https://stackoverflow.com/questions/35151067/algorithm-to-compare-two-images-in-c-sharp) –  Nov 01 '19 at 13:12
  • no of me just want to know if the picture box image matches the circle.png image – Rayane Staszewski Nov 01 '19 at 13:16
  • You can't. The only (reasonable) way is to keep track of what you load to the pbox. (The reason why your code fails is that even though both objects may contain the same image they are still two different objects) – TaW Nov 01 '19 at 14:45

1 Answers1

-1
string filepath = PictureBox.ImageLocation;

If you're loading the image via PictureBox.Image, then Imagelocation will always be null so you may need to change how your loading it for you to achieve what you want.

All that's left is to do a logic check:

if (filepath.ToUpper().Contains("CIRCLE.PNG"))
{
   //do things
}

With that said, if you're actually needing to compare that the images match pixel per pixel, the comment to your OP by Olivier Rogier is a good read about the topic.

Mikael
  • 1,002
  • 1
  • 11
  • 22