title: How to draw multiple rectangles with mouse on an image?
body: <p>I am trying to build a WinForm application that will load an image file from an openfiledialog and load it into either a panel or picturebox.  I want to be able to draw multiple rectangles with the left mouse button and have them stay on the image.  I have been successful in getting a single rectangle at a time on the actual form but not on an image inside a panel/picturebox.  Does anyone know of a resource that would help me understand how to achieve this?</p>
  
  <p>This is the code that allows me to draw a single resizable rectangle on the form, but when I change the mouse events from <code>form1_MouseEvent</code> to <code>panel1_MouseEvent</code> it does nothing..</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.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace Temporary_Name_Utility
  {
      public partial class Form1 : Form
      {
          Rectangle myRectangle;
          bool draw = false;
  
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              try
              {
                  OpenFileDialog myFile = new OpenFileDialog();
                  myFile.Filter = "Image Files(*.img, *.bmp) |*.img; *.bmp;";
                  myFile.InitialDirectory = "c:\\";
  
                  if (myFile.ShowDialog() == DialogResult.OK)
                  {
                      pictureBox1.BackgroundImage = Image.FromFile(myFile.FileName);
                  }
              }
  
              catch (Exception error)
              {
                  MessageBox.Show("Error loading the selected file.  Original error: " + error.Message);
  
              }
  
          }
  
          private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
          {
              myRectangle = new Rectangle(e.X, e.Y, 0, 0);
              this.Invalidate();
          }
  
  
          private void pictureBox1_Paint(object sender, PaintEventArgs e)
          {
              if(draw)
              {
                  using(Pen myPen = new Pen(Color.Red, 2))
                  {
                      e.Graphics.DrawRectangle(myPen, myRectangle);
                  }
              }
          }
  
          private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
          {
              if(e.Button == MouseButtons.Left)
              {
                  myRectangle = new Rectangle(myRectangle.Left, myRectangle.Top, Math.Min(e.X - myRectangle.Left, pictureBox1.ClientRectangle.Width - myRectangle.Left), Math.Min(e.Y - myRectangle.Top, pictureBox1.ClientRectangle.Height - myRectangle.Top));
              }
              this.Invalidate();
          }
  
          private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
          {
  
          }
  
      }
  }
  </code></pre>