Windows Forms' PictureBox with zooming and panning

My application needs control displaying bitmaps (jpg) but also zooming and panning them (so if you press mouse button you can 'move' zoomed picture inside frame)

What I did was placing panel at the Form, then pictureBox inside panel (anchored Top,Left). So if I need zoom it I'm just executing below code from Zoom buttons events:

    private void ZommInOut(bool zoom) {
        int zoomRatio   = 10; // percent
        int widthZoom   = pBox.Width * zoomRatio /100;
        int heightZoom  = pBox.Height  * zoomRatio /100;

        if (zoom) {
            widthZoom   *= -1;
            heightZoom  *= -1;
        }

        pBox.Width  += widthZoom;
        pBox.Height += heightZoom;
    }

Works petty well. Image is zoomed, panel displaying scrollbars - so I have working simple panning functionality.

What is missing to me is possibility to use mouse for panning - I'd like to drag picture in any direction to see other part of picture (as eg Acrobat Reader does). I've looked for the way to use MouseMove event and change scrollbars programically but I couldn't manage that.

Any suggestion(s)?

Answers


You need to set the AutoScrollPosition property in the MouseMove event.

You'll need to track the location of the MouseDown event and update AutoScrollPosition using an offset.


I would suggest creating a control and drawing part of the image using Graphics.DrawImage - in this way you can control how the image is scaled (trilinear etc) and it will also use less memory. You can override OnMouseMove to get the mosue movements


Need Your Help

How can I draw a rectangle over a PDF page?

vb.net winforms pdf

I want to render a PDF page in a control in winforms and then move rectangles around over the PDF to identify user selected text strings. I'm trying to render the PDF using a WebBrowser control but

Get sails request target from policy scope

node.js model-view-controller sails.js

I currently try to set up a simple authorization system (authentication already in place) for a sails (v0.10) application based on the policy system sails provides. For this purpose I'd need to get...