简体   繁体   English

在PictureBox中调整图像的大小,直到达到原始图像的大小为止

[英]Resize an image in a PictureBox until it is the size of the original image

I want an image within a PictureBox that will adjust automatically depending on the size of the window but, not larger than the original image. 我想要一个PictureBox中的图像,该图像将根据窗口的大小自动调整,但不大于原始图像。 While PictureBoxSizeMode.Zoom almost does this, when the PictureBox is larger than the image it causes the image to pixelate. 虽然PictureBoxSizeMode.Zoom几乎可以做到这一点,但是当PictureBox大于图像时,它将导致图像像素化。 I'm currently executing this code on resize: 我目前正在执行以下代码以调整大小:

if (pBox.Height * pBox.Height < pBox.Image.Width * pBox.Image.Height) pBox.SizeMode = PictureBoxSizeMode.Zoom;
else pBox.SizeMode = PictureBoxSizeMode.CenterImage;

While this works most of the time, occasionally the image won't revert back to zoom: 尽管这在大多数情况下都有效,但有时图像不会恢复为缩放状态:

图像仍在“ C​​enterImage”上 全图

Any better way to go about this? 还有更好的方法吗?

How about setting the maximum size to the image size: 如何将最大尺寸设置为图像尺寸:

pictureBox1.MaximumSize = pictureBox1.Image.Size;

In Combination with : 结合:

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

You should use "if (pBox.Width * pBox.Height < pBox.Image.Width * pBox.Image.Height)" if() Statement. 您应该使用“ if(pBox.Width * pBox.Height <pBox.Image.Width * pBox.Image.Height)” if()语句。 Both dimensions of pBox is taken as height, might cause some problem. pBox的两个尺寸均视为高度,可能会引起一些问题。 Check it 核实

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM