在PictureBox调整图像尺寸大,因为它可以去,同时保持高宽比? [英] Resize an image in a PictureBox to as large as it can go, while maintaining aspect ratio?

查看:761
本文介绍了在PictureBox调整图像尺寸大,因为它可以去,同时保持高宽比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个在PictureBox控件的图像会自动调整其大小取决于窗口的大小,但保持纵横比。 SizeMode 到目前为止只设置为 StretchImage 使图像拉伸以适应整个PictureBox控件。这忽略了长宽比,这不是我想要的。

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.

是否有可能保持纵横比,但仍然图像伸展到最大就可以动态地去当窗体的大小变化?是否有可能做到这一点的的有它仍然为中心?我想我可以调整窗口的大小,每次重新创建内存中的形象,而这似乎是一个坏主意。

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 and have 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.

推荐答案

我认为,这是<一的效果href="http://msdn.microsoft.com/en-us/library/system.windows.forms.pictureboxsizemode.aspx"><$c$c>PictureBoxSizeMode.Zoom.该文件说:

I believe that this is the effect of PictureBoxSizeMode.Zoom. The documentation says that:

图像的大小增加或减少保持的尺寸比

The size of the image is increased or decreased maintaining the size ratio.

您设置此对<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode.aspx"><$c$c>PictureBox.SizeMode属性。该功能的文档中的备注一节也说:

You set this on the PictureBox.SizeMode property. The "Remarks" section of the documentation for that function also says:

使用变焦值,使得图像被拉伸或收缩,以适应图片框;然而,在原有的纵横比保持

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.

您可以,当然,将 PictureBox.SizeMode 物业无论是在设计师的属性窗口或code(例如,在窗体的构造函数):

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):

myPictureBox.SizeMode = PictureBoxSizeMode.Zoom;

如果这不会做你想要什么,你总是可以实现调整自己的逻辑。你关注的是,该控件调整大小,每次重新创建图像记忆似乎是一个坏主意,但我不知道为什么它似乎这样对你。唯一的问题是,如果你不小心销毁未使用的图形对象,像老位图。不仅这些对象包含了需要释放非托管资源,你会开始发挥对记忆pressure极端的数额,如果你只是让他们泄露。

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.

另外,为了避免创建临时的位图,你可以做PictureBox控件可能在内部不和使用<一个href="http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage.aspx"><$c$c>Graphics.DrawImage方法来处理拉伸。如果你把它传递一个矩形,它会自动调整图像以适应该矩形内。

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调整图像尺寸大,因为它可以去,同时保持高宽比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆