title: Draw Rectangle inside picture box SizeMode Zoom
body: <p>I have a picture box in a WindowsForms project with its SizeMode to "Zoom".</p>
  
  <p>I want to draw a rectangle inside image and get its coordinates relative to the image and not to the picture box.</p>
  
  <p><strong>The problem is that the rectangle's coordinates do not match with the same rectangle selected on Windows Paint Application.</strong></p>
  
  <p>Here is the code used:</p>
  
  <ol>
  <li><p>Start Painting:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Starts drawing.
  /// &lt;/summary&gt;
  /// &lt;param name="sender"&gt;&lt;/param&gt;
  /// &lt;param name="e"&gt;&lt;/param&gt;
  private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  {
      backupImage = pictureBox1.Image;
      _once = true;
      RectStartPoint = e.Location;
      pictureBox1.Invalidate();
  }
  </code></pre></li>
  <li><p>While moving mouse:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// While moving mouse event, paint rectangle
  /// &lt;/summary&gt;
  /// &lt;param name="sender"&gt;&lt;/param&gt;
  /// &lt;param name="e"&gt;&lt;/param&gt;
  private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  {
      if (_once) //Only draw rectangle while drawing mode
      {
          Point tempEndPoint = e.Location;
          Rect.Location = new Point(Math.Min(RectStartPoint.X, tempEndPoint.X),
              Math.Min(RectStartPoint.Y, tempEndPoint.Y));
  
          Rect = new Rectangle(
              Math.Min(tempEndPoint.X, Rect.Left),
              Math.Min(tempEndPoint.Y, Rect.Top),
              Math.Min(e.X - RectStartPoint.X, pictureBox1.ClientRectangle.Width - RectStartPoint.X),
              Math.Min(e.Y - RectStartPoint.Y, pictureBox1.ClientRectangle.Height - RectStartPoint.Y));
  
          pictureBox1.Refresh();
          pictureBox1.CreateGraphics().DrawRectangle(cropPen, Rect);
      }
  }
  </code></pre></li>
  <li><p>When 2 click, finhish painting rectange:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// When mouse click is released, write in texbox the rectangle's coordinates.
  /// &lt;/summary&gt;
  /// &lt;param name="sender"&gt;&lt;/param&gt;
  /// &lt;param name="e"&gt;&lt;/param&gt;
  private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  {
      if (_once)
      {
          if (e.Button == System.Windows.Forms.MouseButtons.Left)
          {
              Point tempEndPoint = e.Location;
  
              _once = false;
              string sAux = string.Format("Left: {0}; Top: {1}; Width: {2}; Height: {3} \r\n", Math.Min(tempEndPoint.X, Rect.Left), Math.Min(tempEndPoint.Y, Rect.Top),
                      Math.Min(e.X - RectStartPoint.X, pictureBox1.ClientRectangle.Width - RectStartPoint.X), Math.Min(e.Y - RectStartPoint.Y, pictureBox1.ClientRectangle.Height - RectStartPoint.Y));
  
              textBox1.Text += sAux;
          }
      }
  }
  </code></pre></li>
  </ol>
  
  <p>The results are:</p>
  
  <p>Windows Image
  <a href="https://i.stack.imgur.com/6qLUi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6qLUi.png" alt="Windows Image"></a></p>
  
  <p>Paint Image
  <a href="https://i.stack.imgur.com/FIKQw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FIKQw.jpg" alt="enter image description here"></a></p>
  
  <p>As you can see on both images, left, top, width and height do not match.</p>
  
  <p>Can you tell me how to obtain the same result?</p>
  
  <p><a href="https://i.stack.imgur.com/2swJ9.jpg" rel="nofollow noreferrer">Example2</a></p>