PictureBox.Size属性不受赋值的影响

时间:2016-04-13 10:48:52

标签: c# resize picturebox

当我从PictureBox选择文件时,我在tableLayoutPanel3内有Listbox我将pictureBox的大小调整为图片大小:< / p>

private void imageListBox_SelectedIndexChanged(object sender, EventArgs e) {
        System.Diagnostics.Debug.WriteLine(imageListBox.Items[0]);
        string imagePath = imageListBox.SelectedItem + "";
        Bitmap = new Bitmap(imagePath);
        pictureBox.Image = Bitmap;
        pictureBox.Size = Bitmap.Size;//doesn't change pictureBox's size
        //those two sizes below are different
        Debug.WriteLine(Bitmap.Width + " " + Bitmap.Height);
        Debug.WriteLine(pictureBox.Width + " " + pictureBox.Height);
        Invalidate();
    }

Designer

result

问题:如何调整pictureBox的大小以使其适合图像内部?

我的肮脏解决方案,我通过以下方式解决问题: this.Size = new Size(tableLayoutPanel2.Width + Bitmap.Size.Width, Bitmap.Height);但我认为这是一种错误的方式。

编辑:我已经尝试了每个PictureBox.SizeMode值。

1 个答案:

答案 0 :(得分:0)

尝试自动尺寸:

pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;