C# 将 PictureBox 中的图像调整到尽可能大,同时保持纵横比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15534955/
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
Resize an image in a PictureBox to as large as it can go, while maintaining aspect ratio?
提问by Jon
I'm trying to make it so that an image in a PictureBox control will adjust its size automatically depending on the size of the window, but maintain the aspect ratio. So far just setting SizeMode
to StretchImage
causes the image to stretch to fit the entire PictureBox control. This ignores the aspect ratio, which isn't what I want.
我正在尝试使 PictureBox 控件中的图像将根据窗口的大小自动调整其大小,但保持纵横比。到目前为止,只是设置SizeMode
为StretchImage
使图像拉伸以适合整个 PictureBox 控件。这忽略了纵横比,这不是我想要的。
Is it possible to maintain the aspect ratio, but still stretch the image to the largest it can go dynamically as the form's size changes? Is it possible to do this andhave it still be centered? I imagine I could recreate the image in memory each time the window is resized, but this seems like a bad idea.
是否可以保持纵横比,但仍然可以将图像拉伸到它可以随着表单大小变化而动态变化的最大程度?是否有可能做到这一点并让它仍然居中?我想每次调整窗口大小时我都可以在内存中重新创建图像,但这似乎是个坏主意。
采纳答案by Cody Gray
I believe that this is the effect of PictureBoxSizeMode.Zoom
. The documentation says that:
我相信这就是效果PictureBoxSizeMode.Zoom
。文档说:
The size of the image is increased or decreased maintaining the size ratio.
图像的大小增加或减少保持大小比例。
You set this on the PictureBox.SizeMode
property. The "Remarks" section of the documentation for that function also says:
你在PictureBox.SizeMode
属性上设置了这个。该功能的文档的“备注”部分还说:
Using the Zoom value causes the image to be stretched or shrunk to fit the PictureBox; however, the aspect ratio in the original is maintained.
使用 Zoom 值会导致图像被拉伸或收缩以适合 PictureBox;但是,原始的纵横比保持不变。
You can, of course, set the PictureBox.SizeMode
property either in the designer's Properties Window or in code (e.g., in your form's constructor):
当然,您可以PictureBox.SizeMode
在设计器的属性窗口或代码中(例如,在表单的构造函数中)设置属性:
myPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
If this doesn't do exactly what you want, you could always implement the resizing logic yourself. Your concern is that recreating the image in memory each time that the control is resized "seems like a bad idea", but I'm not sure why it seems that way to you. The only problem would be if you weren't careful to destroy unused graphics objects, like the old Bitmap
. Not only do these objects contain unmanaged resources that need to be freed, you'll start exerting extreme amounts of pressure on memory if you just let them leak.
如果这不能完全满足您的要求,您始终可以自己实现调整大小的逻辑。您担心的是,每次调整控件大小时都在内存中重新创建图像“似乎是个坏主意”,但我不确定为什么对您来说是这样。唯一的问题是,如果您不小心销毁未使用的图形对象,例如旧的Bitmap
. 这些对象不仅包含需要释放的非托管资源,而且如果您让它们泄漏,您将开始对内存施加极大的压力。
Alternatively, to avoid creating temporary bitmaps, you can do what the PictureBox control probably does internally and use the Graphics.DrawImage
method to handle the stretching. If you pass it a rectangle, it will automatically scale the image to fit inside of the rectangle.
或者,为了避免创建临时位图,您可以执行 PictureBox 控件可能在内部执行的操作并使用该Graphics.DrawImage
方法处理拉伸。如果传递给它一个矩形,它会自动缩放图像以适应矩形的内部。
回答by ZorleQ
Change the 'SizeMode' to Zoom
.
As the name suggests, "StretchImage" will stretch it to fit the image file. Easy process of elimination would have given you the same result.
将“SizeMode”更改为Zoom
. 顾名思义,“StretchImage”将拉伸它以适合图像文件。简单的消除过程会给你同样的结果。
回答by Daniel A.A. Pelsmaeker
According to the PictureBoxSizeMode
documentationyou can specify PictureBoxSizeMode.Zoom
to get the image to keep its aspect ratio. It will zoom as big as possible without any part of the image overflowing the picture box.
根据PictureBoxSizeMode
文档,您可以指定PictureBoxSizeMode.Zoom
获取图像以保持其纵横比。它将尽可能放大,而不会使图像的任何部分溢出图片框。
And you can play with the Dock
property(setting DockStyle.Fill
) to get the picture box to resize to the size of its container.
您可以使用Dock
属性(setting DockStyle.Fill
) 来让图片框调整到其容器的大小。
回答by dirtyw0lf
You can add it to a Web Browser control. Using Ctrl and mouse button you can zoom in as much as you like.
您可以将其添加到 Web 浏览器控件。使用 Ctrl 和鼠标按钮,您可以随意放大。