How to compare pictures in pictureboxes?

shsh_shah
shsh_shah used Ask the Experts™
on
How to compare 2 pictures in 2 different picture boxes in C#?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Ohh..This will be a lot but i just have to find if the picture in box1,box2 and box3 are same and the picture is from player1. So if the picture selected by player1 are same in 3 concurrent boxes so it should be highlighted thats all.

Something like

if(box1.Image(pic[0])==box2.Image(pic[0])==box3.Image(pic[0])
{
MessageBox.Show("Player 1 is Winner");
}
//To compare the images you'll need to compare them byte by byte so try something like this

MemoryStream imageStream1=new MemoryStream(), imageStream2=new MemoryStream();
            Image img1 = pic1.Image;
            Image img2 = pic2.Image;
            byte[] imageContent1 = new Byte[imageStream1.Length];//Declare a byte array to hold byes read from stream          
            imageStream1.Position = 0; //set the stream position to start            
            imageStream1.Read(imageContent1, 0, (int)imageStream1.Length);// load the byte array with the image

            byte[] imageContent2 = new Byte[imageStream2.Length];
            imageStream2.Position = 0;                      
            imageStream2.Read(imageContent2, 0, (int)imageStream2.Length);
            bool equal = true;

            if (imageStream1.Length == imageStream2.Length)
                for (int i = 0; i < imageContent1.Length; i++)
                {
                    if (imageContent1[i] != imageContent2[i])
                    {
                        equal = false;
                        break;
                    }
                }
            else
                equal = false;

            if (equal)
                MessageBox.Show("Equal");
            else
                MessageBox.Show("Not Equal");
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Author

Commented:
thanks Rahim for the code but how to compare 3 same pictures in a row?

i.e. in TicTacToe game you want to compare that image in 3 boxes are same?
if (imageStream1.Length == imageStream2.Length && imageStream2.Length==imageStream3.Length)
and
if (imageContent1[i] != imageContent2[i] &&imageContent2[i] != imageContent3[i])

Author

Commented:
thanks a million
Welcome!

By the way why dont u keep track of images assigned to picture boxes in some DataStructure like Array, ArrayList etc, so that u know which picturebox has which image.

I mean you can do something like this
struct PictureInfo
{
PictureBox picbox;
string ImageName;
}
and store instances of this struct to some Array or HashTable

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial