title: C# Updating PictureBox image
body: <p>I'm working on a small screen sharing project,and in the main loop(on a second thread) i read data from a connected socket,process it to a small image(kind of a block,with <code>X</code> and <code>Y</code> properties which indicate where to draw that block),and need to redraw it on the previous image-it has to update very fast(at least this is my goal).</p>
  
  <p>So far i created <code>Graphics</code> from the initial image,and used the <code>Graphics.DrawImage</code> method to draw the smaller block(constantly recieved from socket),on the current image.</p>
  
  <p>This is how it looks like:</p>
  
  <pre><code>      private void MainScreenThread()
      {
          ReadData();
          initial = bufferToJpeg();
          while (true)
          {
              int pos = ReadData();
              x = BlockX();
              y = BlockY();
              Bitmap block = bufferToJpeg();
              Draw(block,new Point(x,y));
              this.Invoke(new Action(() =&gt; pictureBox1.Refresh()));         
          }
      }
  
      private void pictureBox1_Paint(object sender, PaintEventArgs e)
      {
          lock (initial)
          {
              e.Graphics.DrawImage(initial, 0, 0);
          }
      }
  </code></pre>
  
  <p>I did some benchmarking,and i've seen that the <code>DrawImage</code> method works quite slow.</p>
  
  <p>An average time to draw a 800X500 image was <strong>~520ms~</strong>.</p>
  
  <p>I decided to implement a faster method myself,using unsafe pointer for faster access.</p>
  
  <pre><code> private  unsafe void Draw(Bitmap bmp2, Point point)
      {
          lock (initial)
          {
              BitmapData bmData = initial.LockBits(new Rectangle(0, 0, initial.Width, initial.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, initial.PixelFormat);
              BitmapData bmData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp2.PixelFormat);
              IntPtr scan0 = bmData.Scan0;
              IntPtr scan02 = bmData2.Scan0;
              int stride = bmData.Stride;
              int stride2 = bmData2.Stride;
              int Width = bmp2.Width;
              int Height = bmp2.Height;
              int X = point.X;
              int Y = point.Y;
  
              for (int y = 0; y &lt; Height; y++)
              {
                  byte* p = (byte*)scan0.ToPointer();
                  p += (Y + y) * stride + X * 4;//setting pointer according the smaller bitmap bounds.
                  byte* p2 = (byte*)scan02.ToPointer();
                  p2 += stride2 * y;
  
                  for (int x = 0; x &lt; Width; x++)
                  {
                      p[0] = p2[0];//B
                      p[1] = p2[1];//G
                      p[2] = p2[2];//R
                      p += 4;//advance pointer +4
                      p2 += 4;//advance pointer +4
                  }
  
              }
              initial.UnlockBits(bmData);
              bmp2.UnlockBits(bmData2);
          }
      }
  </code></pre>
  
  <p>And now the call looks like this:</p>
  
  <pre><code>   while (true)
          {
              int pos = ReadData();
               x = BlockX();
              y = BlockY();
              Bitmap block = bufferToJpeg();
             Draw(block,new Point(x,y));
              this.Invoke(new Action(() =&gt; pictureBox1.Refresh()));         
          }
  </code></pre>
  
  <p>But i keep getting an </p>
  
  <blockquote>
    <p>Object is currently in use elsewhere</p>
  </blockquote>
  
  <p>When i lock the initial image..in this line</p>
  
  <pre><code> BitmapData bmData = initial.LockBits(new Rectangle(0, 0, initial.Width, initial.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, initial.PixelFormat);
  </code></pre>
  
  <p>I'm not sure why this happening..i do not access <code>initial</code> from any other source...</p>
  
  <p>I would appreciate any help.</p>
  
  <p>Thanks.</p>