title: Draggable selection rectangle
body: <p>Before anybody points it out I know that a there is a question with the same title that <a href="https://stackoverflow.com/questions/9374806/winforms-a-draggable-transparent-rectangle">has already been asked here</a> it just doesn't answer my issue I think.</p>
  
  <p>Working in .NET 3.5 As in that question I am making an area selection component to select an area on a picture. The picture is displayed using a custom control in which the picture is drawn during <code>OnPaint</code>.</p>
  
  <p>I have the following code for my selection rectangle:</p>
  
  <pre><code>internal class AreaSelection : Control
  {
      private Rectangle selection
      {
          get { return new Rectangle(Point.Empty, Size.Subtract(this.Size, new Size(1, 1))); }
      }
  
      private Size mouseStartLocation;
  
      public AreaSelection()
      {
          this.Size = new Size(150, 150);
          this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
          this.BackColor = Color.FromArgb(70, 200, 200, 200);
      }
  
      protected override void OnMouseEnter(EventArgs e)
      {
          this.Cursor = Cursors.SizeAll;
  
          base.OnMouseEnter(e);
      }
  
      protected override void OnMouseDown(MouseEventArgs e)
      {
          this.mouseStartLocation = new Size(e.Location);
  
          base.OnMouseDown(e);
      }
  
      protected override void OnMouseMove(MouseEventArgs e)
      {
          if (e.Button == MouseButtons.Left)
          {
              Point offset = e.Location - this.mouseStartLocation;
              this.Left += offset.X;
              this.Top += offset.Y;
          }
  
          base.OnMouseMove(e);
      }
  
      protected override void OnPaint(PaintEventArgs e)
      {
          e.Graphics.DrawRectangle(new Pen(Color.Black) { DashStyle = DashStyle.Dash }, this.selection);
          Debug.WriteLine("Selection redrawn");
      }
  }
  </code></pre>
  
  <p>Which gives me a nice semi-transparent rectangle which I can drag around. The problem I have is that whilst dragging the underlying image which shows through the rectangle gets lags behind the position of the rectangle.</p>
  
  <p>This gets more noticeable the faster I move the rectangle. When I stop moving it the image catches up and everything aligns perfectly again.
  I assume that there is something wrong with the way the rectangle draws, but I really can't figure out what it is...
  Any help would be much appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>I have noticed that the viewer gets redrawn twice as often as the selection area when I drag the selection area. Could this be the cause of the problem?</p>
  
  <p>EDIT 2:</p>
  
  <p>Here is the code for the viewer in case it is relevant:</p>
  
  <pre><code>public enum ImageViewerViewMode
  {
      Normal,
      PrintSelection,
      PrintPreview
  }
  
  public enum ImageViewerZoomMode
  {
      None,
      OnClick,
      Lens
  }
  
  public partial class ImageViewer : UserControl
  {
      /// &lt;summary&gt;
      /// The current zoom factor. Note: Use SetZoom() to set the value.
      /// &lt;/summary&gt;
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
      public float ZoomFactor
      {
          get { return this.zoomFactor; }
          private set
          {
              this.zoomFactor = value;
          }
      }
  
      /// &lt;summary&gt;
      /// The maximum zoom factor to use
      /// &lt;/summary&gt;
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
      public float MaximumZoomFactor
      {
          get
          {
              return this.maximumZoomFactor;
          }
          set
          {
              this.maximumZoomFactor = value;
              this.SetZoomFactorLimits();
          }
      }
  
      /// &lt;summary&gt;
      /// The minimum zoom factort to use
      /// &lt;/summary&gt;
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
      public float MinimumZoomFactor
      {
          get
          {
              return this.minimumZoomFactor;
          }
          set
          {
              this.minimumZoomFactor = value;
              this.SetZoomFactorLimits();
          }
      }
  
      /// &lt;summary&gt;
      /// The multiplying factor to apply to each ZoomIn/ZoomOut command
      /// &lt;/summary&gt;
      [Category("Behavior")]
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
      [DefaultValue(2F)]
      public float ZoomStep { get; set; }
  
      /// &lt;summary&gt;
      /// The image currently displayed by the control
      /// &lt;/summary&gt;
      [Category("Data")]
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
      public Image Image
      {
          get { return this.image; }
          set
          {
              this.image = value;
              this.ZoomExtents();
              this.minimumZoomFactor = this.zoomFactor / 10;
              this.MaximumZoomFactor = this.zoomFactor * 10;
          }
      }
  
      public ImageViewerViewMode ViewMode { get; set; }
  
      public ImageViewerZoomMode ZoomMode { get; set; }
  
      private ImageViewerLens Lens { get; set; }
  
      private float zoomFactor;
      private float minimumZoomFactor;
      private float maximumZoomFactor;
      private bool panning;
      private Point imageLocation;
      private Point imageTranslation;
      private Image image;
      private AreaSelection areaSelection;
  
      /// &lt;summary&gt;
      /// Class constructor
      /// &lt;/summary&gt;
      public ImageViewer()
      {
          this.DoubleBuffered = true;
          this.MinimumZoomFactor = 0.1F;
          this.MaximumZoomFactor = 10F;
          this.ZoomStep = 2F;
          this.UseScannerUI = true;
          this.Lens = new ImageViewerLens();
          this.ViewMode = ImageViewerViewMode.PrintSelection;
          this.areaSelection = new AreaSelection();
          this.Controls.Add(this.areaSelection);
  
          // TWAIN
  
          // Initialise twain
          this.twain = new Twain(new WinFormsWindowMessageHook(this));
          // Try to set the last used default scanner
          if (this.AvailableScanners.Any())
          {
              this.twain.TransferImage += twain_TransferImage;
              this.twain.ScanningComplete += twain_ScanningComplete;
              if (!this.SetScanner(this.defaultScanner))
                  this.SetScanner(this.AvailableScanners.First());
          }
      }
  
      /// &lt;summary&gt;
      /// Saves the currently loaded image under the specified filename, in the specified format at the specified quality
      /// &lt;/summary&gt;
      /// &lt;param name="FileName"&gt;The file name (full file path) under which to save the file. File type extension is not required.&lt;/param&gt;
      /// &lt;param name="Format"&gt;The file format under which to save the file&lt;/param&gt;
      /// &lt;param name="Quality"&gt;The quality in percent of the image to save. This is optional and may or may not be used have an effect depending on the chosen file type. Default is maximum quality.&lt;/param&gt;
      public void SaveImage(string FileName, GraphicFormats Format, uint Quality = 100)
      {
          ImageCodecInfo encoder;
          EncoderParameters encoderParameters;
  
          if (FileName.IsNullOrEmpty())
              throw new ArgumentNullException(FileName);
          else
          {
              string extension = Path.GetExtension(FileName);
              if (!string.IsNullOrEmpty(extension))
                  FileName = FileName.Replace(extension, string.Empty);
              FileName += "." + Format.ToString();
          }
  
          Quality = Math.Min(Math.Max(1, Quality), 100);
  
          if (!TryGetEncoder(Format, out encoder))
              return;
  
          encoderParameters = new EncoderParameters(1);
          encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, (int)Quality);
  
          this.Image.Save(FileName, encoder, encoderParameters);
      }
  
      /// &lt;summary&gt;
      /// Tries to retrieve the appropriate encoder for the chose image format.
      /// &lt;/summary&gt;
      /// &lt;param name="Format"&gt;The image format for which to attempt retrieving the encoder&lt;/param&gt;
      /// &lt;param name="Encoder"&gt;The encoder object in which to store the encoder if found&lt;/param&gt;
      /// &lt;returns&gt;True if the encoder was found, else false&lt;/returns&gt;
      private bool TryGetEncoder(GraphicFormats Format, out ImageCodecInfo Encoder)
      {
          ImageCodecInfo[] codecs;
  
          codecs = ImageCodecInfo.GetImageEncoders();
          Encoder = codecs.First(c =&gt; c.FormatDescription.Equals(Format.ToString(), StringComparison.CurrentCultureIgnoreCase));
  
          return Encoder != null;
      }
  
      /// &lt;summary&gt;
      /// Set the zoom level to view the entire image in the control
      /// &lt;/summary&gt;
      public void ZoomExtents()
      {
          if (this.Image == null)
              return;
  
          this.ZoomFactor = (float)Math.Min((double)this.Width / this.Image.Width, (double)this.Height / this.Image.Height);
          this.LimitBasePoint(imageLocation.X, imageLocation.Y);
          this.Invalidate();
      }
  
      /// &lt;summary&gt;
      /// Multiply the zoom
      /// &lt;/summary&gt;
      /// &lt;param name="NewZoomFactor"&gt;The zoom factor to set for the image&lt;/param&gt;
      public void SetZoom(float NewZoomFactor)
      {
          this.SetZoom(NewZoomFactor, Point.Empty);
      }
  
      /// &lt;summary&gt;
      /// Multiply the zoom
      /// &lt;/summary&gt;
      /// &lt;param name="NewZoomFactor"&gt;The zoom factor to set for the image&lt;/param&gt;
      /// &lt;param name="ZoomLocation"&gt;The point in which to zoom in&lt;/param&gt;
      public void SetZoom(float NewZoomFactor, Point ZoomLocation)
      {
          int x;
          int y;
          float multiplier;
  
          multiplier = NewZoomFactor / this.ZoomFactor;
  
          x = (int)((ZoomLocation.IsEmpty ? this.Width / 2 : ZoomLocation.X - imageLocation.X) / ZoomFactor);
          y = (int)((ZoomLocation.IsEmpty ? this.Height / 2 : ZoomLocation.Y - imageLocation.Y) / ZoomFactor);
  
          if ((multiplier &lt; 1 &amp;&amp; this.ZoomFactor &gt; this.MinimumZoomFactor) || (multiplier &gt; 1 &amp;&amp; this.ZoomFactor &lt; this.MaximumZoomFactor))
              ZoomFactor *= multiplier;
          else
              return;
  
          LimitBasePoint((int)(this.Width / 2 - x * ZoomFactor), (int)(this.Height / 2 - y * ZoomFactor));
          this.Invalidate();
      }
  
      /// &lt;summary&gt;
      /// Determines the base point for positioning the image
      /// &lt;/summary&gt;
      /// &lt;param name="x"&gt;The x coordinate based on which to determine the positioning&lt;/param&gt;
      /// &lt;param name="y"&gt;The y coordinate based on which to determine the positioning&lt;/param&gt;
      private void LimitBasePoint(int x, int y)
      {
          int width;
          int height;
  
          if (this.Image == null)
              return;
  
          width = this.Width - (int)(Image.Width * ZoomFactor);
          height = this.Height - (int)(Image.Height * ZoomFactor);
  
          x = width &lt; 0 ? Math.Max(Math.Min(x, 0), width) : width / 2;
          y = height &lt; 0 ? Math.Max(Math.Min(y, 0), height) : height / 2;
  
          imageLocation = new Point(x, y);
      }
  
      /// &lt;summary&gt;
      /// Verify that the maximum and minimum zoom are correctly set
      /// &lt;/summary&gt;
      private void SetZoomFactorLimits()
      {
          float maximum = this.MaximumZoomFactor;
          float minimum = this.minimumZoomFactor;
  
          this.maximumZoomFactor = Math.Max(maximum, minimum);
          this.minimumZoomFactor = Math.Min(maximum, minimum);
      }
  
      /// &lt;summary&gt;
      /// Mouse button down event
      /// &lt;/summary&gt;
      protected override void OnMouseDown(MouseEventArgs e)
      {
          switch (this.ZoomMode)
          {
              case ImageViewerZoomMode.OnClick:
                  switch (e.Button)
                  {
                      case MouseButtons.Left:
                          this.SetZoom(this.ZoomFactor * this.ZoomStep, e.Location);
                          break;
  
                      case MouseButtons.Middle:
                          this.panning = true;
                          this.Cursor = Cursors.NoMove2D;
                          this.imageTranslation = e.Location;
                          break;
  
                      case MouseButtons.Right:
                          this.SetZoom(this.ZoomFactor / this.ZoomStep, e.Location);
                          break;
                  }
                  break;
  
              case ImageViewerZoomMode.Lens:
                  if (e.Button == MouseButtons.Left)
                  {
                      this.Cursor = Cursors.Cross;
                      this.Lens.Location = e.Location;
                      this.Lens.Visible = true;
                  }
                  else
                  {
                      this.Cursor = Cursors.Default;
                      this.Lens.Visible = false;
                  }
                  this.Invalidate();
                  break;
          }
  
          base.OnMouseDown(e);
      }
  
      /// &lt;summary&gt;
      /// Mouse button up event
      /// &lt;/summary&gt;
      protected override void OnMouseUp(MouseEventArgs e)
      {
          switch (this.ZoomMode)
          {
              case ImageViewerZoomMode.OnClick:
                  if (e.Button == MouseButtons.Middle)
                  {
                      panning = false;
                      this.Cursor = Cursors.Default;
                  }
                  break;
  
              case ImageViewerZoomMode.Lens:
                  break;
          }
  
          base.OnMouseUp(e);
      }
  
      /// &lt;summary&gt;
      /// Mouse move event
      /// &lt;/summary&gt;
      protected override void OnMouseMove(MouseEventArgs e)
      {
          switch (this.ViewMode)
          {
              case ImageViewerViewMode.Normal:
                  switch (this.ZoomMode)
                  {
                      case ImageViewerZoomMode.OnClick:
                          if (panning)
                          {
                              LimitBasePoint(imageLocation.X + e.X - this.imageTranslation.X, imageLocation.Y + e.Y - this.imageTranslation.Y);
                              this.imageTranslation = e.Location;
                          }
                          break;
  
                      case ImageViewerZoomMode.Lens:
                          if (this.Lens.Visible)
                          {
                              this.Lens.Location = e.Location;
                          }
                          break;
                  }
                  break;
  
              case ImageViewerViewMode.PrintSelection:
                  break;
  
              case ImageViewerViewMode.PrintPreview:
                  break;
          }
  
          base.OnMouseMove(e);
      }
  
      /// &lt;summary&gt;
      /// Resize event
      /// &lt;/summary&gt;
      protected override void OnResize(EventArgs e)
      {
          LimitBasePoint(imageLocation.X, imageLocation.Y);
          this.Invalidate();
  
          base.OnResize(e);
      }
  
      /// &lt;summary&gt;
      /// Paint event
      /// &lt;/summary&gt;
      protected override void OnPaint(PaintEventArgs pe)
      {
          Rectangle src;
          Rectangle dst;
  
          pe.Graphics.Clear(this.BackColor);
  
          if (this.Image != null)
          {
              switch (this.ViewMode)
              {
                  case ImageViewerViewMode.Normal:
                      src = new Rectangle(Point.Empty, new Size(Image.Width, Image.Height));
                      dst = new Rectangle(this.imageLocation, new Size((int)(this.Image.Width * this.ZoomFactor), (int)(this.Image.Height * this.ZoomFactor)));
                      pe.Graphics.DrawImage(this.Image, dst, src, GraphicsUnit.Pixel);
  
                      this.Lens.Draw(pe.Graphics, this.Image, this.ZoomFactor, this.imageLocation);
                      break;
  
                  case ImageViewerViewMode.PrintSelection:
                      src = new Rectangle(Point.Empty, new Size(Image.Width, Image.Height));
                      dst = new Rectangle(this.imageLocation, new Size((int)(this.Image.Width * this.ZoomFactor), (int)(this.Image.Height * this.ZoomFactor)));
                      pe.Graphics.DrawImage(this.Image, dst, src, GraphicsUnit.Pixel);
                      break;
  
                  case ImageViewerViewMode.PrintPreview:
                      break;
              }
          }
  
          //Debug.WriteLine("Viewer redrawn " + DateTime.Now);
          base.OnPaint(pe);
      }
  }
  </code></pre>
  
  <p>EDIT 3:</p>
  
  <p>Experience further graphics-related trouble when setting the height to something large. For example, if in the <code>AreaSelection</code> constructor I set the height to 500, dragging the control really screws up the painting.</p>