C#:使用滚动条缩放图片框图像的简单而实用的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18270144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:45:14  来源:igfitidea点击:

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

c#winformsimagepictureboxzooming

提问by Marco Frost

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

编辑:未缩放和缩​​放(16 倍)图像的两个屏幕截图。注意图像上边框与表单上边框之间的边距。 未缩放图像放大图像

采纳答案by Aseem Gautam

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

我认为最好缩放(重新缩放)图像而不是图片框。看看这篇文章 - http://www.codeproject.com/Articles/21097/PictureBox-Zoom

And

How to zoom in&out an image in c#

如何在c#中放大和缩小图像