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
  1. Home
  2. General Programming
  3. C#
  4. Compare picturebox.image with image from a resource file

Compare picturebox.image with image from a resource file

Scheduled Pinned Locked Moved C#
questiontutoriallearning
11 Posts 4 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.
  • D Offline
    D Offline
    dizzyJ
    wrote on last edited by
    #1

    I have added 4 pictures to a resource file which I want to compare with the image in a picture box An example of what I want to do is:

    if (pictureBox1.Image == resourceFile.Image1)
    {
    pictureBox1.Image = resourceFile.Image2;
    }

    else
    {
    pictureBox1.Image = resourceFile.Image1;
    }

    This code compiles but the else-statement always executes. How do I make this work?

    N A S 3 Replies Last reply
    0
    • D dizzyJ

      I have added 4 pictures to a resource file which I want to compare with the image in a picture box An example of what I want to do is:

      if (pictureBox1.Image == resourceFile.Image1)
      {
      pictureBox1.Image = resourceFile.Image2;
      }

      else
      {
      pictureBox1.Image = resourceFile.Image1;
      }

      This code compiles but the else-statement always executes. How do I make this work?

      N Offline
      N Offline
      NavnathKale
      wrote on last edited by
      #2

      try using pictureBox1.Image.Equals

      D 1 Reply Last reply
      0
      • D dizzyJ

        I have added 4 pictures to a resource file which I want to compare with the image in a picture box An example of what I want to do is:

        if (pictureBox1.Image == resourceFile.Image1)
        {
        pictureBox1.Image = resourceFile.Image2;
        }

        else
        {
        pictureBox1.Image = resourceFile.Image1;
        }

        This code compiles but the else-statement always executes. How do I make this work?

        A Offline
        A Offline
        Anindya Chatterjee
        wrote on last edited by
        #3

        That is because the equality operator only valid for the same objects not for the same contents. Though your pictures' contents are same but as they are two different objects in the memory, that is why the control always goes to the else block. Now comparing the two images is not so simple. One way to do is to save the two images in two different MemoryStream using

        var ms = new MemeoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp);
        byte[] byteArray = ms.ToArray();

        Do the same thing for the other image also and then check the equality of two byte arrays by reading each byte. Hope this will solve your problem.

        Regards, Anindya Chatterjee[^]

        N D 2 Replies Last reply
        0
        • N NavnathKale

          try using pictureBox1.Image.Equals

          D Offline
          D Offline
          dizzyJ
          wrote on last edited by
          #4

          Have already tried, it didn't work =( Thanx anyway

          1 Reply Last reply
          0
          • A Anindya Chatterjee

            That is because the equality operator only valid for the same objects not for the same contents. Though your pictures' contents are same but as they are two different objects in the memory, that is why the control always goes to the else block. Now comparing the two images is not so simple. One way to do is to save the two images in two different MemoryStream using

            var ms = new MemeoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp);
            byte[] byteArray = ms.ToArray();

            Do the same thing for the other image also and then check the equality of two byte arrays by reading each byte. Hope this will solve your problem.

            Regards, Anindya Chatterjee[^]

            N Offline
            N Offline
            NavnathKale
            wrote on last edited by
            #5

            Actually i guess Image don't have any implementation for Equals method, so it fails. Byte check should work then.

            1 Reply Last reply
            0
            • A Anindya Chatterjee

              That is because the equality operator only valid for the same objects not for the same contents. Though your pictures' contents are same but as they are two different objects in the memory, that is why the control always goes to the else block. Now comparing the two images is not so simple. One way to do is to save the two images in two different MemoryStream using

              var ms = new MemeoryStream();
              image.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp);
              byte[] byteArray = ms.ToArray();

              Do the same thing for the other image also and then check the equality of two byte arrays by reading each byte. Hope this will solve your problem.

              Regards, Anindya Chatterjee[^]

              D Offline
              D Offline
              dizzyJ
              wrote on last edited by
              #6

              Didn't work! This is what I got from your code example. "Tarning1" is one picture in the resource file. "bild" is the picture box name.

              System.IO.MemoryStream ms1 = new System.IO.MemoryStream();
              Bitmap pic1 = new Bitmap(bild.Image);
              pic1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
              byte[] byteArray1 = ms1.ToArray();

              System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
              Bitmap pic2 = DicePictures.Tarning1;
              pic2.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
              byte[] byteArray2 = ms2.ToArray();

              if (byteArray1 == byteArray2)
              Console.WriteLine("Equal");
              else
              Console.WriteLine("Not equal");

              Did I miss something?

              A 1 Reply Last reply
              0
              • D dizzyJ

                Didn't work! This is what I got from your code example. "Tarning1" is one picture in the resource file. "bild" is the picture box name.

                System.IO.MemoryStream ms1 = new System.IO.MemoryStream();
                Bitmap pic1 = new Bitmap(bild.Image);
                pic1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] byteArray1 = ms1.ToArray();

                System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
                Bitmap pic2 = DicePictures.Tarning1;
                pic2.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] byteArray2 = ms2.ToArray();

                if (byteArray1 == byteArray2)
                Console.WriteLine("Equal");
                else
                Console.WriteLine("Not equal");

                Did I miss something?

                A Offline
                A Offline
                Anindya Chatterjee
                wrote on last edited by
                #7

                Some remarks for your code. 1. Use Image class directly instead of Bitmap. And don't use Jpeg format use MemoryBmp, it will ease byte comparing.

                bild.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.MemoryBmp);

                2. Don't use

                if(byteArray1 == byteArray2)

                coz you are doing the same wrong thing again. I told you to compare byte by byte using looping like as follows

                if(byteArray1.Length == byteArray2.Length)
                {
                for(int i = 0; i < byteArray1.Length; i++)
                {
                if(byteArray1[i] != byteArray2[i])
                return false;
                }
                return true;
                }
                return false;

                Hope this will give you the desired result.

                Regards, Anindya Chatterjee[^]

                D 1 Reply Last reply
                0
                • A Anindya Chatterjee

                  Some remarks for your code. 1. Use Image class directly instead of Bitmap. And don't use Jpeg format use MemoryBmp, it will ease byte comparing.

                  bild.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.MemoryBmp);

                  2. Don't use

                  if(byteArray1 == byteArray2)

                  coz you are doing the same wrong thing again. I told you to compare byte by byte using looping like as follows

                  if(byteArray1.Length == byteArray2.Length)
                  {
                  for(int i = 0; i < byteArray1.Length; i++)
                  {
                  if(byteArray1[i] != byteArray2[i])
                  return false;
                  }
                  return true;
                  }
                  return false;

                  Hope this will give you the desired result.

                  Regards, Anindya Chatterjee[^]

                  D Offline
                  D Offline
                  dizzyJ
                  wrote on last edited by
                  #8

                  We tried that but,

                  bild.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.MemoryBmp);

                  Returns an ArgumentNullException. "Value cannot be null, parameter name: encoder". I guess the MemoryBmp ImageFormat is null or something. Didn't you get this exception?

                  A 1 Reply Last reply
                  0
                  • D dizzyJ

                    We tried that but,

                    bild.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.MemoryBmp);

                    Returns an ArgumentNullException. "Value cannot be null, parameter name: encoder". I guess the MemoryBmp ImageFormat is null or something. Didn't you get this exception?

                    A Offline
                    A Offline
                    Anindya Chatterjee
                    wrote on last edited by
                    #9

                    Use System.Drawing.Imaging.ImageFormat.Bmp instead. Here is the test code

                    Image img1 = pictureBox1.Image;
                    Image img2 = pictureBox2.Image;

                            MemoryStream ms1 = new MemoryStream(), ms2 = new MemoryStream();
                            
                            img1.Save(ms1, ImageFormat.Bmp);
                            img2.Save(ms2, ImageFormat.Bmp);
                    
                            byte\[\] byteArray1, byteArray2;
                    
                            byteArray1 = ms1.ToArray();
                            byteArray2 = ms2.ToArray();
                    
                            if (byteArray1.Length == byteArray2.Length)
                            {
                                for (int i = 0; i < byteArray1.Length; i++)
                                {
                                    if(byteArray1\[i\] != byteArray2\[i\])
                                        MessageBox.Show("Not Same");
                                }
                                MessageBox.Show("Image Same");
                            }
                            else
                                MessageBox.Show("Not Same");
                    

                    Inform me if it solves the problem.

                    Regards, Anindya Chatterjee[^]

                    D 1 Reply Last reply
                    0
                    • A Anindya Chatterjee

                      Use System.Drawing.Imaging.ImageFormat.Bmp instead. Here is the test code

                      Image img1 = pictureBox1.Image;
                      Image img2 = pictureBox2.Image;

                              MemoryStream ms1 = new MemoryStream(), ms2 = new MemoryStream();
                              
                              img1.Save(ms1, ImageFormat.Bmp);
                              img2.Save(ms2, ImageFormat.Bmp);
                      
                              byte\[\] byteArray1, byteArray2;
                      
                              byteArray1 = ms1.ToArray();
                              byteArray2 = ms2.ToArray();
                      
                              if (byteArray1.Length == byteArray2.Length)
                              {
                                  for (int i = 0; i < byteArray1.Length; i++)
                                  {
                                      if(byteArray1\[i\] != byteArray2\[i\])
                                          MessageBox.Show("Not Same");
                                  }
                                  MessageBox.Show("Image Same");
                              }
                              else
                                  MessageBox.Show("Not Same");
                      

                      Inform me if it solves the problem.

                      Regards, Anindya Chatterjee[^]

                      D Offline
                      D Offline
                      dizzyJ
                      wrote on last edited by
                      #10

                      It didn't work, but I will find another way to do it. Thank you for all your help.

                      1 Reply Last reply
                      0
                      • D dizzyJ

                        I have added 4 pictures to a resource file which I want to compare with the image in a picture box An example of what I want to do is:

                        if (pictureBox1.Image == resourceFile.Image1)
                        {
                        pictureBox1.Image = resourceFile.Image2;
                        }

                        else
                        {
                        pictureBox1.Image = resourceFile.Image1;
                        }

                        This code compiles but the else-statement always executes. How do I make this work?

                        S Offline
                        S Offline
                        sadiq_arbaoui
                        wrote on last edited by
                        #11

                        Try this one!! it works for me!

                        bool b = false;
                        for (int i = 0; i < 255; i++)
                        {
                        if (pictureBox_OK.Image.Palette.Entries[i] == Properties.Resources.OK.Palette.Entries[i])
                        {
                        b = true;
                        }
                        else
                        {
                        b = false;
                        break; // break the ( for (int i = 0; i < 255; i++))
                        }
                        }

                        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