views:

379

answers:

2

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)?

+2  A: 

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.

SLaks
Whose MouseMove event you recommend for that? Form's or Panel's ?
Maciej
@Maciej i don't think you're moving your mouse over the form. Your MouseMove ultimately will be captured by the PictureBox.
Veer
Sorry I've made mistake - I'd like to ask about Panel of PictureBox (not a Form). But you are right PB is right place. Thanks
Maciej
A: 

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

James Westgate
You are probably right. But I've looked for something quick and codeless. Mentioned solution works to me.
Maciej