简体   繁体   中英

Autosize Image in Picture Box

I'm doing an imageviewer. I've already done importing the image in the picture box.

Which code should be used to autosize the image in the picture box? Here's my code in Viewing the image in picture box.

  private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Multiselect = true;
        openFileDialog.Filter = "JPEG|*.jpg|Bitmaps|*.bmp";

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            pFileNames = openFileDialog.FileNames;
            pCurrentImage = 0;
            ImageView();
        }
    }

    protected void ImageView()
    {
        if (pCurrentImage >= 0 && pCurrentImage <= pFileNames.Length - 1)
        {
            pictureBox1.Image = Bitmap.FromFile(pFileNames[pCurrentImage]);
        }
    }

Take a look at the SizeMode property of the PictureBox : http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode.aspx

Set this to AutoSize and you're ready to go.

Check here what you can set it to

Check PictureBox.SizeMode Property and set it by PictureBoxSizeMode Enumeration as you want PictureBox control to do while displaying the image.

  • AutoSize means that the PictureBox is going to fit to the image.

If you want image fit to the pictureBox then set the sizemode to StretchImage

// Set the SizeMode property to the StretchImage value.  
// This will enlarge the image as needed to fit into 
// the PictureBox.
    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

您可以尝试将PictureBox的SizeMode属性设置为AutoSize。

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