Introduction

There are several articles about panning and zooming, but to do both of these at the same time can be more useful to the user of your program. But, this does take a little getting used to since you can pan your image too much if you are not used to it.

Background

This is not a new idea, CAD programs use it all the time. But, I don't usually see this feature in imaging software, and since it can be useful, I decided to write one.

Playing With the Code

Try a click drag, to pan the image, and use the mouse wheel to zoom in and out.

Then, try zooming in to a point of interest like an eye or something, and see how the pan-zoom works. It will take some getting used to because you can easily over shoot your target.

Using the Code

Since it is not too long or complicated, I just posted the important parts below. Some things to note are I use buffered graphics for this demo, and I didn't go through extensive error checking situations. I did test this and tried to block some common bugs.

Think of an image on a really big piece of paper, this is our world coordinates. Then, take a window that looks at the world. You can move the window left or right to see different parts of the world. You can move the window forwards or backwards (zooming) to adjust how much of the world you can see. This is the basic concept behind the world coordinates and the viewport coordinates below.

Points of Interest

A couple of things. First, you will notice that zooming in can be blocky or clunky at times; this is because the pan is moving large amounts. I reduced this by dividing by 2, but it is noticeable. To fix this and make it more smooth, you can either make the maximum pan amount a constant or some proportion of the zoom level. The method posted above accomplished the goal, so I did not pursue the finer details. Note that I create a class whose entire existence and purpose is to override the OnPaintBackground of the standard Panel object. This is done to remove the flicker created by a normal Panel doing the background drawing.

History

This is the second program like this. Since this version performs better, I posted it.

Share

About the Author

Currently I work on industrial machinery as a mechanic. I have a few other things on my resume before that are mostly computer related. I have been writing programs in various languages for about 10 years. The single most complex thing I have ever encountered as a programmer, an engineering student, a Christian, a husband, a soldier, and father is, Jesus The Christ, and the book called The Bible. It is like a masterful fractal, but like a graphical representation of a fractal you do not have to understand the math to see how simple, beautiful, and complex it is. Yes, the physical bible is a bunch of words on paper. However, to understand what The Bible says is a scary thing. The world you now live in becomes like a movie studio set, of plywood cutouts. And to look behind the cutout and understand that things are not what they seem is a real mind bender. I could keep going, but the Bible is the smallest most efficient way to communicate the information inside. Note: It is easy to read The Bible and not understand what it means. It is impossible to understand The Bible unless God allows you to, I am not being impetuous, but until you find your life the things you say and do, today, in that book. You may close your eyes and think you know what it means to be blind, but if you have ever been blind you don't 'think you know' you understand.