title: Why i'm getting exception InvalidOperationException when using backgroundworker?
body: <p>The exception message is: Bitmap region is already locked and it's in Program.cs on the line:</p>
  
  <pre><code>Application.Run(new Form1());
  </code></pre>
  
  <p>The exception is connected with the method: get_images_with_clouds</p>
  
  <p>If i'm not using the backgroundworker and in the form1 constructor i did:</p>
  
  <pre><code>bitmaps = ImagesComparison.get_images_with_clouds(b);
  </code></pre>
  
  <p>It was working fine but when i'm doing this line in the backgroundworker do work event i'm getting this exception.</p>
  
  <p>In a new form designer i added a new backgroundworker1
  In the new form constructor i did:</p>
  
  <pre><code>b = new Bitmap(InitGifFile);
  pictureBox1.Image = b;
  backgroundWorker1.RunWorkerAsync();
  </code></pre>
  
  <p>b is a Bitmap variable and InitGifGile is a string variable</p>
  
  <p>The exception started only when i started to use the backgroundworker.
  Then in the backgroundworker dowork event i did:</p>
  
  <pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              BackgroundWorker bgw = (BackgroundWorker)sender;
                  if (bgw.CancellationPending == true)
                  {
  
                  }
                  else
                  {
                      bitmaps = ImagesComparison.get_images_with_clouds(b);
                  }
          }
  </code></pre>
  
  <p>And the method get_images_with_clouds:</p>
  
  <pre><code>public static Bitmap[] get_images_with_clouds(Bitmap radar_image)
          {
              int e = 0;
              int f = 0;
              int image_clock_area_x = 0;
              int image_clock_area_y = 0;
              int image_clock_area_x1 = 140;
              int image_clock_area_y1 = 21;
              Bitmap[] localImages;
              localImages = new Bitmap[15];
              Bitmap image;
              image = new Bitmap(Properties.Resources.radar_without_clouds);
              BitmapData bmD = null;
              BitmapData bmD2 = null;
  
              try
              {
                  bmD = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                  bmD2 = radar_image.LockBits(new Rectangle(0, 0, radar_image.Width, radar_image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                  IntPtr sc0 = bmD.Scan0;
  
                  unsafe
                  {
                      int* p = (int*)sc0.ToPointer();
                      int* p2 = (int*)bmD2.Scan0.ToPointer();
  
                      for (e = image_clock_area_x; e &lt; image_clock_area_x + image_clock_area_x1; e++)
                      {
                          for (f = image_clock_area_y; f &lt; image_clock_area_y + image_clock_area_y1; f++)
                          {
                              Color clock_color = Color.FromArgb(p2[e + f * bmD2.Width]);
                              p[e + f * bmD.Width] = clock_color.ToArgb();
                          }
                      }
                  }
  
                  image.UnlockBits(bmD);
                  radar_image.UnlockBits(bmD2);
              }
              catch
              {
                  try
                  {
                      image.UnlockBits(bmD);
                  }
                  catch
                  {
  
                  }
                  try
                  {
                      radar_image.UnlockBits(bmD2);
                  }
                  catch
                  {
  
                  }
              }
  
              int c;
              for (c = 0; c &lt; localImages.Length; c++)
              {
                  localImages[c] = new Bitmap(image);
              }
  
              Bitmap new_image = new Bitmap(Properties.Resources.radar_without_clouds);
              Bitmap new_image1 = new Bitmap(Properties.Resources.radar_without_clouds);
              Bitmap localbmptest = black_and_white(new_image, radar_image);
              Image image1 = black_and_white(new_image, radar_image);
              image1.Save(@"c:\temp\testclouds666.jpg");
              Bitmap clouds = new Bitmap(image1);
              int x;
              int y;
              int a;
              int b;
              int d = 0;
              Bitmap redImage;
              redImage = new Bitmap(512, 512);
              using (Graphics g = Graphics.FromImage(redImage))
              {
                  g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                  g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                  g.Clear(Color.Red);
              }
  
              BitmapData bmData = null;
              BitmapData bmData2 = null;
              BitmapData bmDataArray = null;
  
              try
              {
                  bmData = clouds.LockBits(new Rectangle(0, 0, clouds.Width, clouds.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                  bmData2 = radar_image.LockBits(new Rectangle(0, 0, radar_image.Width, radar_image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
  
                  IntPtr scan0 = bmData.Scan0;
                  IntPtr scan02 = bmData2.Scan0;
  
                  unsafe
                  {
                      int* p = (int*)scan0.ToPointer();
                      int* p2 = (int*)scan02.ToPointer();
  
                      double h, mm;
  
                      for (d = 0; d &lt; localImages.Length; d++)
                      {
                          bmDataArray = localImages[d].LockBits(new Rectangle(0, 0, localImages[d].Width, localImages[d].Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  
                          IntPtr scan0Array = bmDataArray.Scan0;
                          int* pArray = (int*)scan0Array.ToPointer();
  
                          for (a = 0; a &lt; new_image.Width; a++)
                          {
                              for (b = 0; b &lt; new_image.Height; b++)
                              {
                                  Color color1 = Color.FromArgb(p[a + b * bmData.Width]); 
                                  Color color2 = Color.FromArgb(p2[a + b * bmData2.Width]); 
  
                                  if (color1.R != 0 || color1.G != 0 || color1.B != 0)
                                  {
                                      h = color2.GetHue();
                                      mm = RadarAnalysis.Hue2MMPerHour(h);
                                      if (mm &gt;= treshhold_array[14 - d])
                                      {
                                          pArray[a + b * bmDataArray.Width] = color2.ToArgb();
                                      }
  
                                  }
                              }
                          }
  
                          localImages[d].UnlockBits(bmDataArray);
                      }
                  }
                  clouds.UnlockBits(bmData);
                  radar_image.UnlockBits(bmData2);
  
              }
              catch (Exception error)
              {
                  try
                  {
                      clouds.UnlockBits(bmData);
                  }
                  catch
                  {
  
                  }
                  try
                  {
                      radar_image.UnlockBits(bmData2);
                  }
                  catch
                  {
  
                  }
                  try
                  {
                      localImages[d].UnlockBits(bmDataArray);
                  }
                  catch
                  {
  
                  }
                  Logger.Write("Error Exception ==&gt; " + error);
                  MessageBox.Show("Error Exception ==&gt; " + error);
              }
              return localImages;
  
          }
  </code></pre>
  
  <p>The full exception error message:</p>
  
  <pre><code>System.InvalidOperationException was unhandled
    HResult=-2146233079
    Message=Bitmap region is already locked.
    Source=System.Drawing
    StackTrace:
         at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
         at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
         at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
         at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
         at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
         at System.Windows.Forms.Control.WmPaint(Message&amp; m)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    InnerException: 
  </code></pre>
  
  <p>I'm not sure why when using the backgroundworker it's making this exception but without the backgroundworker it's working fine with the same line.</p>