Clipping the image
What should happen when you enlarge the image on the screen? In most circumstances the image should be constrained to a region of the application UI. As you zoom into the picture it stays within that region. In WPF we can accomplish this with a clipping region. Certain elements like the Border expose a ClipToBounds property which makes this task even easier.
As you can see the ClipToBounds property is enabled on the border. The image is nested inside the border which permits the border to clip off the edges of the picture. Notice also the RenderTransform I've applied to the image. I am going to use the ScaleTransform to perform the zoom and the TranslateTransform to manipulate the image while panning. I gave both these transforms a name to make it easier to manipulate them in the code behind.
The Mouse events
I'll use the mouse wheel event to determine when to zoom the picture.
For the panning I'll work with three mouse events; MouseLeftButtonDown, MouseLeftButtonUp and MouseMove.
I'll define two point variables to use during the move.
Then it is simply a matter of setting up the three mouse events as follows:
Application Screenshots
Photo for article is from Yngve Lundström Photography.
This was first published in July 2010