Question Getting graphics transformations to work together.


Well-known member
Aug 12, 2008
Programming Experience
Hi everyone, this is my first post here.

I'm trying to make a form where the user can freely pan, rotate and zoom a large image using the mouse. I can combine any two of these functions but I cannot find a way of getting all three of them to work together. I don't want to store an intermediate image (which might be one way of dealing with it) because that would slow things down far too much: a 10 megapixel image zoomed to 50x would take a lot of storing! So I am trying to do everything with transformations.

This is my approach. The mouse event handlers set a panning origin (PanOrigin As Point), a zoom factor (ZoomFactor As Single) and a rotation angle (RotationDegrees As Single). For the moment, I want to zoom and rotate around a stationary point at the centre of the window (mp As Point).

In the Paint event handler, I have put the following code:

e.Graphics.TranslateTransform(-mp.X, -mp.Y, MatrixOrder.Append)
e.Graphics.RotateTransform(RotateDegrees, MatrixOrder.Append)
e.Graphics.ScaleTransform(ZoomFactor, ZoomFactor, MatrixOrder.Append)
e.Graphics.TranslateTransform(mp.X, mp.Y, MatrixOrder.Append)
e.Graphics.DrawImage(TestImage, PanOrigin.X, PanOrigin.Y)
This works - almost. The image rotates and zooms around the intended centre, but panning doesn't work properly. Once the image has been rotated, it goes skidding off in its own direction instead of the direction you try to drag it in. After much experimentation I found I could straighten out the panning action by using Graphics.TransformPoints. I replaced the Graphics.DrawImage statement above by the following lines:
Dim points() As Point = {PanOrigin}
e.Graphics.TransformPoints(CoordinateSpace.Device, CoordinateSpace.World, points)
e.Graphics.TranslateTransform(points(0).X - mp.X, points(0).Y - mp.Y, MatrixOrder.Append)
e.Graphics.Drawimage(TestImage, 0, 0)
But now the intended stationary point isn't fixed. It moves along with the image when you drag it.

I have tried all kinds of other things but I have not succeeded in getting both the panning straight and the stationary point stationary. So I'm hoping there's a transformations expert around here who can advise me.

Regards, Vic Joseph
Top Bottom