I have a picture box set to AutoSize so that the image forces it to grow to the image's full size.
The picture box is in a panel with autoScroll = true, so that scroll bars appear when the picture is larger than the panel.
How can I programmatically scroll the panel as the user clicks the drags on the image, thereby repositioning the image.
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.
The does move the image around, but it jumps all over the place, and scrolls unpredictably.
How can I achieve this?
Here's what I have in my Mouse events...
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (dragging) { if (e.Button == MouseButtons.Left) { int horizontalChange = (e.X - startingX) * -1; // move the image inverse to direction dragged int newHorizontalPos = panel1.HorizontalScroll.Value + horizontalChange; if (newHorizontalPos < panel1.HorizontalScroll.Minimum) { newHorizontalPos = panel1.HorizontalScroll.Minimum; horizontalChange = 0; } if (newHorizontalPos > panel1.HorizontalScroll.Maximum) { newHorizontalPos = panel1.HorizontalScroll.Maximum; horizontalChange = 0; } panel1.HorizontalScroll.Value = newHorizontalPos; int verticalChange = (e.Y - startingY); int newverticalPos = panel1.VerticalScroll.Value + verticalChange * -1; // move the image inverse to direction dragged if (newverticalPos panel1.VerticalScroll.Maximum) { newverticalPos = panel1.VerticalScroll.Maximum; verticalChange = 0; } panel1.VerticalScroll.Value = newverticalPos; } } startingX = e.X; startingY = e.Y; }
Is my logic wrong or is my understanding of the panel's scrolling functionality wrong?