title: How can I cause a panel to scroll programatically to expose its AutoSized picture box
body: <p>I have a picture box set to AutoSize so that the image forces it to grow to the image's full size.</p>
  
  <p>The picture box is in a panel with autoScroll = true, so that scroll bars appear when the picture is larger than the panel.</p>
  
  <p>How can I programmatically scroll the panel as the user clicks the drags on the image, thereby repositioning the image.</p>
  
  <p>I've tried used the MouseMove event, capturing the last X and Y positions of the mouse, calculating how much the mouse has moved, and adjusted the Vertical and Horizontal Scroll values of the panel.</p>
  
  <p>The does move the image around, but it jumps all over the place, and scrolls unpredictably.</p>
  
  <p>How can I achieve this?</p>
  
  <p>Here's what I have in my Mouse events...</p>
  
  <pre><code>private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  {
      if (dragging)
      {
          if (e.Button == MouseButtons.Left)
          {
              // move the image inverse to direction dragged
              int horizontalChange = (e.X - startingX) * -1;  
              int newHorizontalPos = panel1.HorizontalScroll.Value + horizontalChange;
  
              if (newHorizontalPos &amp;lt; panel1.HorizontalScroll.Minimum)
              {
                  newHorizontalPos = panel1.HorizontalScroll.Minimum;
                  horizontalChange = 0;
              }
  
              if (newHorizontalPos &amp;gt; panel1.HorizontalScroll.Maximum)
              {
                  newHorizontalPos = panel1.HorizontalScroll.Maximum;
                  horizontalChange = 0;
              }
  
              panel1.HorizontalScroll.Value = newHorizontalPos;
  
              int verticalChange = (e.Y - startingY);
              // move the image inverse to direction dragged
              int newverticalPos = panel1.VerticalScroll.Value + verticalChange * -1;  
  
              if (newverticalPos &lt; panel1.VerticalScroll.Minimum)
              {
                  newverticalPos = panel1.VerticalScroll.Minimum;
                  verticalChange = 0;
              }
  
              if (newverticalPos &gt; panel1.VerticalScroll.Maximum)
              {
                  newverticalPos = panel1.VerticalScroll.Maximum;
                  verticalChange = 0;
              }
  
              panel1.VerticalScroll.Value = newverticalPos;
          }
      }
  
      startingX = e.X;
      startingY = e.Y;
  }
  </code></pre>
  
  <p>Is my logic wrong or is my understanding of the panel's scrolling functionality wrong?</p>