Search code examples
c#imagecomparepictureboximagelist

Compare images to see if they're the same


I'm trying to create a slot machine, i have 3 empty pictureboxes and a image list with a bunch of different pictures in it, i use a random number generator to put images into the picturebox from the image list.

Now how do i compare to see if the three random pictures are matching?

picturebox1.image == picturebox2.image; 
//doesnt work because names aren't loaded to image property

picturebox1.imagelocation == picture2.imagelocation
//doesn't work because all images come from the same place.

I also can't try comparing the size or the extension because they are all the same I don't want to use multiple random number generators to select the random pictures and compare the different random numbers. Is there a trick i can do with the imagelist that i haven't thought of


Solution

  • One option would be to use the Tag property... many classes have one, including Bitmap, Image, and PictureBox. You could assign a unique value to each Image.Tag...

    var bmp = new Bitmap(1,1);
    bmp.Tag = "uniqueTag";
    pictureBox1.Image.Tag = bmp;   // pictureBox1.Image.Tag == "uniqueTag"
    

    ... then check for equality:

    if (pictureBox1.Image.Tag == pictureBox2.Image.Tag)
    {
        ...
    }