Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Comparing Two PictureBox

Comparing Two PictureBox

Scheduled Pinned Locked Moved C#
questiongame-devhelplounge
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gamer1127
    wrote on last edited by
    #1

    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.

    L OriginalGriffO 3 Replies Last reply
    0
    • G gamer1127

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      G 1 Reply Last reply
      0
      • G gamer1127

        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.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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"

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        G 1 Reply Last reply
        0
        • L Lost User

          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.

          G Offline
          G Offline
          gamer1127
          wrote on last edited by
          #4

          Oh, i forgot to say that the images that the two pictureBox get came from an imagelist.

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            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"

            G Offline
            G Offline
            gamer1127
            wrote on last edited by
            #5

            How can i do the bitwise comparison if the images i get came from an imageList?

            L OriginalGriffO 2 Replies Last reply
            0
            • G gamer1127

              How can i do the bitwise comparison if the images i get came from an imageList?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The same way as if they came from anywhere else

              1 Reply Last reply
              0
              • G gamer1127

                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.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Does the MD5 Hash Code Work for you ? MD5 is embedded in C# .NET try to get hash for the two images

                I know nothing , I know nothing ...

                1 Reply Last reply
                0
                • G gamer1127

                  How can i do the bitwise comparison if the images i get came from an imageList?

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  G 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    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"

                    G Offline
                    G Offline
                    gamer1127
                    wrote on last edited by
                    #9

                    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

                    OriginalGriffO 1 Reply Last reply
                    0
                    • G gamer1127

                      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

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      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"

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups