简体   繁体   中英

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. While PictureBoxSizeMode.Zoom almost does this, when the PictureBox is larger than the image it causes the image to pixelate. 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. Both dimensions of pBox is taken as height, might cause some problem. Check it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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