title: Drawing a rectangle on a picture box
body: <p>When this form opens, it has a background image of the fullscreen and the user can mouse down and draw a rectangle which will be used later to crop the background image. The problem I am getting is that when the form paints, it's not drawing a rectangle so the user doesnt know the the area they are selecting</p>
  
  <p>This is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace Quick_Screenshot
  {
      public partial class Crop : Form
      {
          private Rectangle croprect = Rectangle.Empty;
          private bool mouseDown = false;
          private bool selectedArea = false;
          Point sp, ep;
          Bitmap background;
  
          public Rectangle CropArea
          {
              get { return croprect; }
          }
  
          public bool SelectedArea
          {
              get { return selectedArea; }
          }
  
          public Crop(Bitmap image)
          {
              InitializeComponent();
  
              background = image;
              picBox.MouseDown += new MouseEventHandler(Crop_MouseDown);
              picBox.MouseUp += new MouseEventHandler(Crop_MouseUp);
              picBox.MouseMove += new MouseEventHandler(Crop_MouseMove);
              picBox.Paint += new PaintEventHandler(Crop_Paint);
  
          }
  
          private void Crop_Load(object sender, EventArgs e)
          {
              picBox.Image = (Image)background;
          }
  
          private void Crop_MouseDown(object sender, MouseEventArgs e)
          {
              if (e.Button == System.Windows.Forms.MouseButtons.Right)
                  this.Close();
              else
              {
                  mouseDown = true;
                  sp = ep = e.Location;
              }
          }
  
          private void Crop_MouseUp(object sender, MouseEventArgs e)
          {
              ep = e.Location;
              mouseDown = false;
              croprect = GetRectangle(sp, ep);
  
              if (croprect.Width &gt; 10 &amp;&amp; croprect.Height &gt; 10)
              {
                  selectedArea = true;
              }
              else
              {
                  croprect = Rectangle.Empty;
                  Taskbar.Balloon("Selected area too small", "Quick Screenshot", ToolTipIcon.Error);
              }
  
              this.Close();
          }
  
          private void Crop_MouseMove(object sender, MouseEventArgs e)
          {
              if (mouseDown)
              {
                  ep = e.Location;
                  Update();
              }
          }
  
          private void Crop_Paint(object sender, PaintEventArgs e)
          {
              e.Graphics.DrawRectangle(Pens.Red, GetRectangle(sp, ep));
              e.Graphics.Save();
          }
  
          private Rectangle GetRectangle(Point p1, Point p2)
          {
              return new Rectangle(
                  Math.Min(p1.X, p2.X),
                  Math.Min(p1.Y, p2.Y),
                  Math.Abs(p1.X - p2.X),
                  Math.Abs(p1.Y - p2.Y)
                  );
          }
      }
  }
  </code></pre>