title: C# Bitmap region is already locked
body: <p>I need to process a single image very constantly and i use another thread(apart from the main ui) to do it. Actually i get a RGB array and i turn it into a Bitmap-That's the initial first process,after that im getting(from a socket if it's really interest you :P) a smaller array which contains only the changed at certain position,somthing like deltas. I need to apply this rgb array everytime on the initial image and update it according to the rgb component in each array.
  This is not my problem now, this stage was done already, my main problem is when i send it to a method which <strong>Lock the bitmap and process it</strong> it throws  An </p>
  
  <blockquote>
    <p>Bitmap region is already locked.</p>
  </blockquote>
  
  <p>Error after few seconds of running.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public void retreive()
      {
              byte[] res = decompress( ReceiveVarData(s));//receiving the array.              
              MemoryStream ms = new MemoryStream(res);//storing it in a ms.
              BinaryReader br = new BinaryReader(ms);//use binaryreader to read from it.
              Bitmap first = FirstProcess(br);              
              pictureBox1.Image =new Bitmap( first);
            //this is the initial part, the first process.
              current = first;                
             while (true)
              {
                  byte[] del = ReceiveVarData(s);                 
                 current = DeltaProcessing(del);
                  pictureBox1.Image =  current;
  
              }              
      }
  
      Bitmap current;
      private unsafe Bitmap DeltaProcessing(byte []rgb)
      {          
        BitmapData  bmData = current.LockBits(new System.Drawing.Rectangle(0, 0, 1920,1080), System.Drawing.Imaging.ImageLockMode.ReadWrite,PixelFormat.Format32bppRgb);
        IntPtr scan0 = bmData.Scan0;
  
          for (int i=0;i&lt;rgb.Length;i+=7)
          {
              int offset = BitConverter.ToInt32(rgb, i);
                  byte* p = (byte*)((int)(scan0) + offset);
                  p[0] = rgb[i+4];
                  p[1] = rgb[i+5];
                  p[2] = rgb[i+6];
                  p[3] = 255;
  
         }
  
         current.UnlockBits(bmData);
          return current;
  
      }
  
  
        private void Form1_Load(object sender, EventArgs e)
      {  
          tl.Start();
          cl = tl.AcceptTcpClient();
          s = cl.Client;
          Thread th = new Thread(retreive);
          th.Start();
      }
  </code></pre>
  
  <p>As you see,i simply loop to process the image and apply the new rgb values.
  As i mentioned before-<strong>this is not the problem</strong> the very odd problem is that i keep getting the <code>bitmap region is already locked</code>  error  eventhough i make sure i <code>Unlock</code> the bitmap everytime after the process.. 
  Any ideas?</p>