简体   繁体   中英

PictureBox not sizing properly

I created a PictureBox and load an image into it, and I wanted the pictures to have a maximum size (let's say 250px). Here is the code I'm using at that moment

PictureBox cellPictureBox = new PictureBox();
                            cellPictureBox.AutoSize = false;
                            cellPictureBox.Dock = DockStyle.Fill;
                            cellPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                            cellPictureBox.Image = Base64ToImage(data.ToString().Trim());

                            cellPictureBox.Width = 250;
                            cellPictureBox.Height = 250;

When I load the first image, it's quite large. When I load a second image, it's automatically scaled down to be very tiny, and the newly loaded image takesthe large size the first image had. This trend continues as I add more rows to my data. 在此处输入图片说明

What can I do to help manage the sizing of my images? Making a custom control I've been told is useful, but I don't see what is happening that I have wrong.

To set maximum and minimum sizes, have a look at the following:

PictureBox cellPictureBox = new PictureBox();
cellPictureBox.AutoSize = false;
cellPictureBox.Dock = DockStyle.Fill;
cellPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
cellPictureBox.Image = Base64ToImage(data.ToString().Trim());

cellPictureBox.MinimumSize = new Size(100, 100); // or whatever size you want.
cellPictureBox.MaximumSize = new Size(250, 250);

If you only want to set a limit on one dimension, for example, the width, but the height is allowed to be anything, then use int.MaxValue:

cellPictureBox.MaximumSize = new Size(250, int.MaxValue);

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