Proper way to zoom and pan images?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I would like to re-create the zooming and panning functionality in "Windows Picture and Fax Viewer" (the default image viewer in XP) in my application.<br/> <br/> Currently, my form essentially just contains a PictureBox object and Im capturing some of the Mouse events on this control in order to implement this functionality. <br/> <br/> To accomplish the "zoom effect", Autoscroll has been turned on and the OnMouseWheel event has been overridden so that it controls zoom instead of scroll. When the mousewheel scrolls, the picturebox is resized appropriately (and the form itself remains the same size), and the upper-left hand point ("pictureBox.Location") is moved as well. This results in a "zoom effect" where the center of the image is the focal point (as opposed to the upper-left point, which is the focal point if the location of the pictureBox is not changed). This works basically how I would like it to.<br/> <br/> What does not work how I would like, however, is my current pan functionality. What I am doing is capturing the "MouseDown" event, recording the mouse position, and comparing this position to the mouse position after the "MouseUp" event. The difference is then used to move the pictureBox accordingly. This works, but feels a bit awkward when using the application. It would feel much better if the image pictureBox would move with the mouse cursor (instead of waiting for the "MouseUp" event). How is this accomplished?<br/> <br/> One other issue I have is that when either zooming or panning, when the pictureBoxs location point is moved to outside of the form (via making the values negative), the scroll bars do not allow me to scroll left/up, but will allow me to scroll right/down when the bottom-right point is far outside of the form. How do I fix this?<br/> <br/> I will go ahead and post my current code as a reply to this thread. Am I missing something or is there a much better way to do this? <br/> <br/> Thanks for any suggestions you may have.

View the full article
 
Back
Top