title: C# check images are equal with tolerance
body: <p>I have this code where I send images from a thermal camera. <code>getImage()</code> returns the actual image that is provided by the camera. There is no possibility to check directly if the camera can provide a 'new' image, so I did this method to compare two images:  </p>
  
  <pre><code>class ImageCompare
      {
          public enum CompareResult
          {
              CompareOK,
              SizeMismatch,
              PixelMismatch
          };
  
          public static CompareResult CompareImages(Image i1, Image i2)
          {
              CompareResult cr = CompareResult.CompareOK;
  
              if (i1.Size != i2.Size)
              {
                  cr = CompareResult.SizeMismatch;
              }
              else
              {
                  ImageConverter ic = new ImageConverter();
                  byte[] btImage1 = new byte[1];
                  btImage1 = (byte[])ic.ConvertTo(i1, btImage1.GetType());
                  byte[] btImage2 = new byte[1];
                  btImage2 = (byte[])ic.ConvertTo(i2, btImage2.GetType());
  
                  //compute hashes
                  SHA256Managed shaM = new SHA256Managed();
                  byte[] hash1 = shaM.ComputeHash(btImage1);
                  byte[] hash2 = shaM.ComputeHash(btImage2);
  
                  for (int i = 0; i &lt; hash1.Length &amp;&amp; i &lt; hash2.Length
                                    &amp;&amp; cr == CompareResult.CompareOK; i++)
                  {
                      if (hash1[i] != hash2[i])
                          cr = CompareResult.PixelMismatch;
                  }
              }
              return cr;
          }
      }
  </code></pre>
  
  <p>and here is how I use this class: </p>
  
  <pre><code>private static void HandleImageSending(Socket client, Socket s)
          {
              int sent;
              int imageCount = 0;
              long totalSize = 0;
              try
              {
                  while (true)
                  {
                      Console.WriteLine("Starting sending...");
                      Image old = getImage();
                      byte[] bmpBytes;
                      using (Image bmp = getImage())
                      using (MemoryStream ms = new MemoryStream())
                      {
                          if (ImageCompare.CompareImages(bmp, old) == ImageCompare.CompareResult.CompareOK)
                          {
                              bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                              bmpBytes = ms.ToArray();
                              sent = SendVarData(client, bmpBytes);
                              imageCount++;
                              totalSize += sent;
                              old = bmp;
                          }
                      }
                  }
              }
              catch (Exception e)
              { ... }
  </code></pre>
  
  <p>So my problem is that comparing by hash results in 
  'different' images in about 19 of 20 cases. Since the camera provides only 8 fps, there must be something wrong.</p>
  
  <p>Is there a posibilty of comparing with a kind of tolerance, so maybe lets say 5 or 10 percent of the compared new image may differ to the old?  </p>
  
  <p>Since this is used on a mini PC, I would like to use as less CPU load as possible.<br>
  Is there anyone who can help me out here?</p>