title: C# find differences between images
body: <p>I'm trying to compare screenshots and write only the differences.</p>
  
  <p>This is my code</p>
  
  <pre><code>public Bitmap Difference(Bitmap bmp0, Bitmap bmp1)
  {
          Bitmap bmp2;
          int Bpp = bmp0.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;
  
          bmp2 = new Bitmap(bmp0.Width, bmp0.Height, bmp0.PixelFormat);
  
          var bmpData0 = bmp0.LockBits(
                          new Rectangle(0, 0, bmp0.Width, bmp0.Height),
                          ImageLockMode.ReadOnly, bmp0.PixelFormat);
          var bmpData1 = bmp1.LockBits(
                          new Rectangle(0, 0, bmp1.Width, bmp1.Height),
                          ImageLockMode.ReadOnly, bmp1.PixelFormat);
          var bmpData2 = bmp2.LockBits(
                          new Rectangle(0, 0, bmp2.Width, bmp2.Height),
                          ImageLockMode.ReadWrite, bmp2.PixelFormat);
  
         // MessageBox.Show(bmpData0.Stride.ToString());
         int len = bmpData0.Height * bmpData0.Stride;
  
         //   MessageBox.Show(bmpData0.Stride.ToString());
         bool changed = false;
  
         byte[] data0 = new byte[len];
         byte[] data1 = new byte[len];
         byte[] data2 = new byte[len];
  
         Marshal.Copy(bmpData0.Scan0, data0, 0, len);
         Marshal.Copy(bmpData1.Scan0, data1, 0, len);
         Marshal.Copy(bmpData2.Scan0, data2, 0, len);
  
         for (int i = 0; i &lt; len; i += Bpp)
         {
              changed = ((data0[i] != data1[i])
                            || (data0[i + 1] != data1[i + 1])
                            || (data0[i + 2] != data1[i + 2]));
  
              // this.Invoke(new Action(() =&gt; this.Text = changed.ToString()));
  
              data2[i] = changed ? data1[i] : (byte)2;   // special markers
              data2[i + 1] = changed ? data1[i + 1] : (byte)3;   // special markers
              data2[i + 2] = changed ? data1[i + 2] : (byte)7;   // special markers
  
              if (Bpp == 4) 
                 data2[i + 3] = changed ? (byte)255 : (byte)42;  // special markers
      }
  
      //  this.Invoke(new Action(() =&gt; this.Text = changed.ToString()));
      Marshal.Copy(data2, 0, bmpData2.Scan0, len);
      bmp0.UnlockBits(bmpData0);
      bmp1.UnlockBits(bmpData1);
      bmp2.UnlockBits(bmpData2);
  
      return bmp2;
  }
  
  
      Bitmap shot = new Bitmap(SystemInformation.VirtualScreen.Width,
                      SystemInformation.VirtualScreen.Height,
                      PixelFormat.Format24bppRgb);
  
      public Bitmap screenshot()
      {
  
  
          Graphics screenGraph = Graphics.FromImage(shot);
          screenGraph.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                     Screen.PrimaryScreen.Bounds.Y,
                                     0,
                                     0,
                                     SystemInformation.VirtualScreen.Size,
                                     CopyPixelOperation.SourceCopy);
  
          return shot;
  
      }
  </code></pre>
  
  <p>And my call:</p>
  
  <pre><code>private void Form1_Load(object sender, EventArgs e)
  {
          Bitmap prev = screenshot();
  
          Thread.Sleep(1000);
          Bitmap  curr = screenshot();
  
          pictureBox1.Image=Difference(prev,curr);
  }
  </code></pre>
  
  <p>im getting an <code>Bitmap region is already locked.</code> <strong>Error</strong> in this line </p>
  
  <pre><code>   var bmpData1 = bmp1.LockBits(
                          new Rectangle(0, 0, bmp1.Width, bmp1.Height),
                          ImageLockMode.ReadOnly, bmp1.PixelFormat);
  </code></pre>
  
  <p>this is my screenshot code, a simple .net screen capture method:</p>
  
  <p>it's realy weird because i dont even used LockBits in the <code>screenshot</code> method so i have no idea why it's throwing this error...</p>