没有错误但无法在Windows窗体中加载图像

时间:2011-05-25 12:25:54

标签: c# winforms bitmapimage

代码在这里,设计师窗口有一个按钮和一个图片框!

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();

    dlg.Title = "Open Image";
    dlg.Filter = "bmp files (*.bmp)|*.bmp";

    if (dlg.ShowDialog() == DialogResult.OK)
    {

        PictureBox PictureBox1 = new PictureBox();
        PictureBox1.Image = Image.FromFile(dlg.FileName);
        /* PictureBox1.Image = new Bitmap(dlg.FileName);

            // Add the new control to its parent's controls collection
            this.Controls.Add(PictureBox1);
            //dlg.Dispose();*/
    }
}

窗口打开时没有错误,当我按下按钮打开目录,然后选择图像,但是无法在窗口中加载图像。图像im加载是49.6 MB,这会产生任何问题。

3 个答案:

答案 0 :(得分:3)

你已经注释掉了你将图片框添加到窗口的部分,id还建议设置图片框的一些基本宽度/高度,以便你可以确定它在屏幕上显示。

它是否适用于更小的测试图像?

答案 1 :(得分:1)

不是动态创建图片框,而是将它放在Designer中的任何位置,并将其Visible属性设置为False。

假设您将其命名为PictureBox1,只需在按下按钮时指定其图像,而不创建任何新图片框,此外将其可见性更改为true

PictureBox1.Image = Image.FromFile(dlg.FileName);
PictureBox1.Visible = true;

使用当前代码,图片框的默认位置为0,0,表示窗口的左上角。

答案 2 :(得分:0)

尝试

PictureBox.Image = new Bitmap(dlg.FileName);
PictureBox.SizeMode = PictureBoxSizeMode.StretchImage;