C# 图片框的图像缩放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12646287/
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 00:00:05  来源:igfitidea点击:

image Scaling of picture box

c#imageimage-scaling

提问by pila

i got a problem with image scaling in C#.

我在 C# 中遇到图像缩放问题。

I have a picture Box with given Size : e.g. width = 800px height = 600px

我有一个给定大小的图片框:例如宽度 = 800px 高度 = 600px

I am loading different images into that picture box, small ones ( 400x400) and big ones (800+ x 600+)

我正在将不同的图像加载到该图片框中,小(400x400)和大(800+ x 600+)

My images are getting resized if they do not fit into box. But they are alwaysresized to MAX width and height of PictureBox. So the aspect ratio is destroyed.

如果它们不适合框,我的图像将被调整大小。但是它们总是被调整为图片框的最大宽度和高度。所以纵横比被破坏了

Can anybody help to identify / fix the problem?

任何人都可以帮助识别/解决问题吗?

Classes:

课程:

Form1.cs

表格1.cs

ImageHandling.cs(commented out)

ImageHandling.cs(注释掉)

ImageUtilities.cs

ImageUtilities.cs

Examples:

例子:

Problem 1: My Version
enter image description here

问题 1:我的版本
在此处输入图片说明

vs Original Source enter image description here

与原始来源 在此处输入图片说明

Problem 2:
My Version
enter image description here

问题 2:
我的版本
在此处输入图片说明

vs Original Source
enter image description here

与原始来源
在此处输入图片说明

How i want it:

我想要它:

Solution
enter image description here

解决方案
在此处输入图片说明

采纳答案by Forte L.

this.PictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

Set that property to your PictureBox and the size of the image will increased or decreased to fit the PictureBox maintaining the size ratio.

将该属性设置为您的图片框,图像的大小将增加或减少以适合保持大小比例的图片框。

For more info: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode.aspx

更多信息:http: //msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode.aspx