Comparing Two PictureBox
-
Hello. I'm having some problems about comparing two
pictureBox
. I want the other one to be invisible when they have the same image but when I tried it didn't have any changes. How can I do that? Me and my groupmates are creating a game similar to mystery case files: prime suspect and I'm the one who is creating the two player mode. The purpose of making the visible property of the other pictureBox is to let the players know that they already found the image we're asking for them to find but until now I can't still do that. Please help me. Here's the code I tried to use, this is just one of the pictureBox i want to compare to the other pictureBox:private void picBoxRandom1_Click(object sender, EventArgs e)
{
if (picBoxPlayerOne1st.Image.Equals(picBoxRandom1.Image))
{
picBoxRandom1.Visible = false;
PlayerTwo();
}if (picBoxPlayerTwo1st.Image.Equals(picBoxRandom1.Image)) { picBoxRandom1.Visible = false; PlayerOne(); } }
picBoxPlayerOne1st is the pictureBox where the asked image is being shown while the picBoxRandom1 is the pictureBox where the right image is shown. Both pictureBox have random images.
-
Hello. I'm having some problems about comparing two
pictureBox
. I want the other one to be invisible when they have the same image but when I tried it didn't have any changes. How can I do that? Me and my groupmates are creating a game similar to mystery case files: prime suspect and I'm the one who is creating the two player mode. The purpose of making the visible property of the other pictureBox is to let the players know that they already found the image we're asking for them to find but until now I can't still do that. Please help me. Here's the code I tried to use, this is just one of the pictureBox i want to compare to the other pictureBox:private void picBoxRandom1_Click(object sender, EventArgs e)
{
if (picBoxPlayerOne1st.Image.Equals(picBoxRandom1.Image))
{
picBoxRandom1.Visible = false;
PlayerTwo();
}if (picBoxPlayerTwo1st.Image.Equals(picBoxRandom1.Image)) { picBoxRandom1.Visible = false; PlayerOne(); } }
picBoxPlayerOne1st is the pictureBox where the asked image is being shown while the picBoxRandom1 is the pictureBox where the right image is shown. Both pictureBox have random images.
Image.Equals does a reference comparison and it will always return false since they may be pointing to two different Image objects in memory, even if you loaded them from the same file. You can achieve this behavior by setting a flag during the loading of these images into the picture boxes by checking if they are loaded from the same file.
-
Hello. I'm having some problems about comparing two
pictureBox
. I want the other one to be invisible when they have the same image but when I tried it didn't have any changes. How can I do that? Me and my groupmates are creating a game similar to mystery case files: prime suspect and I'm the one who is creating the two player mode. The purpose of making the visible property of the other pictureBox is to let the players know that they already found the image we're asking for them to find but until now I can't still do that. Please help me. Here's the code I tried to use, this is just one of the pictureBox i want to compare to the other pictureBox:private void picBoxRandom1_Click(object sender, EventArgs e)
{
if (picBoxPlayerOne1st.Image.Equals(picBoxRandom1.Image))
{
picBoxRandom1.Visible = false;
PlayerTwo();
}if (picBoxPlayerTwo1st.Image.Equals(picBoxRandom1.Image)) { picBoxRandom1.Visible = false; PlayerOne(); } }
picBoxPlayerOne1st is the pictureBox where the asked image is being shown while the picBoxRandom1 is the pictureBox where the right image is shown. Both pictureBox have random images.
The problem is that the PictureBox.Equals method[^] is inherited from object, and is a reference test (as PictureBox is a reference object). What that means in English is that Equals compares the references (in C or C++ that would be a pointer) rather than comparing them bit - by - bit. If the pointers are to different ememory, then the Equals test will return false even if the two images are bitwise identical. You will have to do a bitwise comparison to check they are realy different.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
-
Image.Equals does a reference comparison and it will always return false since they may be pointing to two different Image objects in memory, even if you loaded them from the same file. You can achieve this behavior by setting a flag during the loading of these images into the picture boxes by checking if they are loaded from the same file.
-
The problem is that the PictureBox.Equals method[^] is inherited from object, and is a reference test (as PictureBox is a reference object). What that means in English is that Equals compares the references (in C or C++ that would be a pointer) rather than comparing them bit - by - bit. If the pointers are to different ememory, then the Equals test will return false even if the two images are bitwise identical. You will have to do a bitwise comparison to check they are realy different.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
-
Hello. I'm having some problems about comparing two
pictureBox
. I want the other one to be invisible when they have the same image but when I tried it didn't have any changes. How can I do that? Me and my groupmates are creating a game similar to mystery case files: prime suspect and I'm the one who is creating the two player mode. The purpose of making the visible property of the other pictureBox is to let the players know that they already found the image we're asking for them to find but until now I can't still do that. Please help me. Here's the code I tried to use, this is just one of the pictureBox i want to compare to the other pictureBox:private void picBoxRandom1_Click(object sender, EventArgs e)
{
if (picBoxPlayerOne1st.Image.Equals(picBoxRandom1.Image))
{
picBoxRandom1.Visible = false;
PlayerTwo();
}if (picBoxPlayerTwo1st.Image.Equals(picBoxRandom1.Image)) { picBoxRandom1.Visible = false; PlayerOne(); } }
picBoxPlayerOne1st is the pictureBox where the asked image is being shown while the picBoxRandom1 is the pictureBox where the right image is shown. Both pictureBox have random images.
-
As Harold says, same way as anything else: iterate through the bytes of the image and compare! Won't be quick though! The MD5 suggestion is not bad - it should be quick, and it will identify non-identical pictures. It could however give false positives. If this is a game, then I assume you have a limited number of different pictures? Take the MD5 hash of each of them and modify the pictures to ensure they all have different MD5 hashes. Then use the MD5 to identify whether the picture is the same, or not. Moves the processing work to offline, so it doesn't become noticable at run time.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
-
As Harold says, same way as anything else: iterate through the bytes of the image and compare! Won't be quick though! The MD5 suggestion is not bad - it should be quick, and it will identify non-identical pictures. It could however give false positives. If this is a game, then I assume you have a limited number of different pictures? Take the MD5 hash of each of them and modify the pictures to ensure they all have different MD5 hashes. Then use the MD5 to identify whether the picture is the same, or not. Moves the processing work to offline, so it doesn't become noticable at run time.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
I used a code that uses memorystream so i can save the image to a specific location then convert it to base64String and use that location to compare the two images. I think it does the work i want so i think i'm okay with that. I'll just use the md5 hash code as a backup in case something goes wrong..anyway thanks for the suggestions
-
I used a code that uses memorystream so i can save the image to a specific location then convert it to base64String and use that location to compare the two images. I think it does the work i want so i think i'm okay with that. I'll just use the md5 hash code as a backup in case something goes wrong..anyway thanks for the suggestions
If you already have the image as a MemoryStream, then why go to all that bother? Isn't it simpler to use
byte[] bytes = ms.GetBuffer();
and then compare the two buffers directly?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"