Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. UnzoomedImageZoomedImage

like image 764
Marco Frost Avatar asked Aug 16 '13 09:08

Marco Frost


1 Answers

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#

like image 84
Aseem Gautam Avatar answered Sep 24 '22 12:09

Aseem Gautam