C#: Simple and functional way to zoom picturebox images with scroll bars

Marco Frost picture Marco Frost · Aug 16, 2013 · Viewed 74.1k times · Source

Is there a simple and functional way to zoom an image in a picturebox including scroll bars?

At the moment, I use a picture box in a panel with auto scroll activated. To zoom, I enlarge the picturebox and move it with the scroll bars on the panel. The problem is, that it behaves strange. For example: If you zoom in to far, the margin between the upper and left form border and the image get's bigger and bigger.

That's the zooming method. I got it from here.

private void ZoomInOut(bool zoom)
    {
        //Zoom ratio by which the images will be zoomed by default
        int zoomRatio = 10;
        //Set the zoomed width and height
        int widthZoom = pictureBox_viewer.Width * zoomRatio / 100;
        int heightZoom = pictureBox_viewer.Height * zoomRatio / 100;
        //zoom = true --> zoom in
        //zoom = false --> zoom out
        if (!zoom)
        {
            widthZoom *= -1;
            heightZoom *= -1;
        }
        //Add the width and height to the picture box dimensions
        pictureBox_viewer.Width += widthZoom;
        pictureBox_viewer.Height += heightZoom;

    }

Any help is appreciated.

Thanks in advance.

Marco

EDIT: Two screenshots of an unzoomed and a zoomed (16 times) image. Pay attention to the margin between the upper border of the image and the upper border of the form. UnzoomedImage ZoomedImage

Answer

Aseem Gautam picture Aseem Gautam · Aug 16, 2013

I think its better to zoom(rescale) the image and not the picture box. Take a look at this article - http://www.codeproject.com/Articles/21097/PictureBox-Zoom

And

How to zoom in&out an image in c#