title: How would you keep from panning past the edges of a PictureBox control nested inside a Panel while zooming?
body: <p>I would like to know how I can alter my code to keep from panning past the edges of the PictureBox while it is zoomed in and when it's in normal state. If possible I would also like to know how to make it zoom at the mouse's current location it is hovering and keep the image quality while zooming. Any Help would greatly be appreciated.</p>
  
  <p>Here is the code for zooming:</p>
  
  <pre><code>Private Sub PictureBox_MouseWheel(sender As System.Object,
                               e As MouseEventArgs) Handles PictureBox1.MouseWheel
      If e.Delta &lt;&gt; 0 Then
          If e.Delta &lt;= 0 Then
              If PictureBox1.Width &lt; 500 Then Exit Sub 
          Else
              If PictureBox1.Width &gt; 2000 Then Exit Sub 
          End If
  
          PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)
          PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)
      End If
  
  End Sub
  </code></pre>
  
  <p>And here is the code for Panning I am using:</p>
  
  <pre><code>Private Offset As Point
      Private Sub Picturebox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
          Offset = e.Location
      End Sub
      Private Sub Picturebox1_MouseMove(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
      PictureBox1.Select()
      If e.Button = Windows.Forms.MouseButtons.Left Then
          PictureBox1.Left += e.X - Offset.X
          PictureBox1.Top += e.Y - Offset.Y
      End If
      End Sub
  </code></pre>