title: How do I improve the speed of my C# algorithm by using bytes instead of GetPixel() method?
body: <p>I have a function in C# which compare two images and finds the differences between them. The problem is that it is too slow. How can I optimize the code by using bytes instead of GetPixel() method?</p>
  
  <p>The speed of this calculus is 0.34 seconds and I should do it in at least ten times less. Some colleague told me I can improve the speed by using bytes.  </p>
  
  <pre><code>private void btnGo_Click(object sender, EventArgs e)
  {
      this.Cursor = Cursors.WaitCursor;
      Application.DoEvents();
  
      // Load the images.
      Bitmap bm1 = (Bitmap)picImage1.Image;
      Bitmap bm2 = (Bitmap)picImage2.Image;
  
      // Make a difference image.
      int wid = Math.Min(bm1.Width, bm2.Width);
      int hgt = Math.Min(bm1.Height, bm2.Height);
      Bitmap bm3 = new Bitmap(wid, hgt);
  
      // Create the difference image.
      bool are_identical = true;
      Color eq_color = Color.White;
      Color ne_color = Color.Red;
      for (int x = 0; x &lt; wid; x++)
      {
          for (int y = 0; y &lt; hgt; y++)
          {
               if (bm1.GetPixel(x, y).Equals(bm2.GetPixel(x, y)))
                  bm3.SetPixel(x, y, eq_color);
               else
               {
                  bm3.SetPixel(x, y, ne_color);
                  are_identical = false;
               }
          }
      }
  
      // Display the result.
      picResult.Image = bm3;
  
      this.Cursor = Cursors.Default;
      if ((bm1.Width != bm2.Width) || (bm1.Height != bm2.Height)) 
      are_identical = false;
      if (are_identical)
          lblResult.Text = "The images are identical";
      else
          lblResult.Text = "The images are different";
    }
  </code></pre>
  
  <p>By using bytes I should be able to have this computation in 0.04 seconds.</p>