title: Graphics.DrawImage speed
body: <p>In my program, I'm coding a basic image editor. Part of this allows the user to draw a rectangular region and I pop up a display that shows that region zoomed by 3x or so (which they can adjust further with the mouse wheel). If they right click and drag this image, it will move the zoom region around on the original image, basically acting as a magnifying glass.</p>
  
  <p>The problem is, I'm seeing some serious performance issues even on relatively small bitmaps. If the bitmap showing the zoomed region is around 400x400 it's still updating as fast as mouse can move and is perfectly smooth, but if I mouse wheel the zoom up to around 450x450, it immediately starts chunking, only down to around 2 updates per second, if that. I don't understand why such a small increase incurs such an enormous performance problem... it's like I've hit some internal memory limit or something. It doesn't seem to matter the size of the source bitmap that is being zoomed, just the size of the zoomed bitmap.</p>
  
  <p>The problem is that I'm using Graphics.DrawImage and a PictureBox. Reading around this site, I see that the performance for both of these is typically not very good, but I don't know enough about the inner workings of GDI to improve my speed. I was hoping some of you might know where my bottlenecks are, as I'm likely just using these tools in poor ways or don't know of a better tool to use in its place.</p>
  
  <p>Here are some snippets of my mouse events and related functions.</p>
  
  <pre><code>private void pictureBox_MouseDown(object sender, MouseEventArgs e)
      {
  
          else if (e.Button == System.Windows.Forms.MouseButtons.Right)
          {
              // slide the zoomed part to look at a different area of the original image
              if (zoomFactor &gt; 1)
              {
                  isMovingZoom = true;
                  // try saving the graphics object?? are these settings helping at all??
                  zoomingGraphics = Graphics.FromImage(displayImage);
                  zoomingGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                  zoomingGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                  zoomingGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
                  zoomingGraphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
              }
          }
      }
  
  
  private void pictureBox_MouseMove(object sender, MouseEventArgs e)
      {
          if (isMovingZoom)
          {
              // some computation on where they moved mouse ommitted here
  
              zoomRegion.X = originalZoomRegion.X + delta.X;
              zoomRegion.Y = originalZoomRegion.Y + delta.Y;
              zoomRegionEnlarged = scaleToOriginal(zoomRegion);
  
              // overwrite the existing displayImage to prevent more Bitmaps being allocated
              createZoomedImage(image.Bitmap, zoomRegionEnlarged, zoomFactor, displayImage, zoomingGraphics);
          }
      }
  
  private void createZoomedImage(Bitmap source, Rectangle srcRegion, float zoom, Bitmap output, Graphics outputGraphics)
      {
          Rectangle destRect = new Rectangle(0, 0, (int)(srcRegion.Width * zoom), (int)(srcRegion.Height * zoom));
  
              outputGraphics.DrawImage(source, destRect, srcRegion, GraphicsUnit.Pixel);
  
          if (displayImage != originalDisplayImage &amp;&amp; displayImage != output)
              displayImage.Dispose();
          setImageInBox(output);
      }
  
  // sets the picture box image, as well as resizes the window to fit
      void setImageInBox(Bitmap bmp)
      {
          pictureBox.Image = bmp;
          displayImage = bmp;
          this.Width = pictureBox.Width + okButton.Width + SystemInformation.FrameBorderSize.Width * 2 + 25;
          this.Height = Math.Max(450, pictureBox.Height) + SystemInformation.CaptionHeight + SystemInformation.FrameBorderSize.Height * 2 + 20;
      }
  
  private void pictureBox_MouseUp(object sender, MouseEventArgs e)
      {
          else if (e.Button == System.Windows.Forms.MouseButtons.Right)
          {
              if (isMovingZoom)
              {
                  isMovingZoom = false;
                  zoomingGraphics.Dispose();
              }
          }
      }
  </code></pre>
  
  <p>As you can see, I'm not declaring a new Bitmap every time I want to draw something, I'm reusing an old Bitmap (and the Bitmap's graphics object, though I don't know if there is much cost with calling Graphics.FromImage repeatedly). I tried adding Stopwatches around to benchmark my code, but I think DrawImage passes functionality to another thread so the function claims to be done relatively quickly. I'm trying to <code>Dispose</code> all my Bitmap and Graphics objects when I'm not using them, and avoid repeated calls to allocate/deallocate resources during the <code>MouseMove</code> event. I'm using a <code>PictureBox</code> but I don't think that's the problem here.</p>
  
  <p>Any help to speed up this code or teach me what's happening in DrawImage is appreciated! I've trimmed some excess code to make it more presentable, but if I've accidentally trimmed something important, or don't show how I'm using something which may be causing problems, please let me know and I'll revise the post.</p>