Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

winforms - 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. UnzoomedImage ZoomedImage

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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#


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...