使用打开文件对话框将位图图像加载到窗体中!

时间:2011-05-25 10:27:21

标签: c# .net winforms bitmap picturebox

我需要使用打开的文件对话框打开窗口中的位图图像(我将从驱动器加载它)。图像应该放在图片框中。这里有一些代码我试过但是出错了!

 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(dlg.FileName);
            }

            dlg.Dispose();
        }

8 个答案:

答案 0 :(得分:31)

您必须使用Bitmap class创建constructor overload的实例,该using statement从磁盘上的文件加载图像。在您的代码编写完成后,您尝试使用PictureBox.Image 属性,就好像它是方法一样。

将您的代码更改为这样(同时利用Controls collection确保正确处理,而不是手动调用Dispose方法):

private void button1_Click(object sender, EventArgs e)
{
    // Wrap the creation of the OpenFileDialog instance in a using statement,
    // rather than manually calling the Dispose method to ensure proper disposal
    using (OpenFileDialog dlg = new OpenFileDialog())
    {
        dlg.Title = "Open Image";
        dlg.Filter = "bmp files (*.bmp)|*.bmp";

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            PictureBox PictureBox1 = new PictureBox();

            // Create a new Bitmap object from the picture file on disk,
            // and assign that to the PictureBox.Image property
            PictureBox1.Image = new Bitmap(dlg.FileName);
        }
    }
}

当然,这不会在表单上的任何位置显示图像,因为您创建的图片框控件尚未添加到表单中。您需要使用Add method将刚刚创建的新图片框控件添加到表单{{3}}。请注意这里添加到上面代码的行:

private void button1_Click(object sender, EventArgs e)
{
    using (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 = new Bitmap(dlg.FileName);

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

答案 1 :(得分:8)

工作正常。  试试这个,

private void addImageButton_Click(object sender, EventArgs e)
{
    OpenFileDialog of = new OpenFileDialog();
    //For any other formats
    of.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG"; 
    if (of.ShowDialog() == DialogResult.OK)
    {
        pictureBox1.ImageLocation = of.FileName;

    }
}

答案 2 :(得分:6)

你应该尝试:

  • 以形式直观地创建图片框(更容易)
  • 将图片框的Dock属性设置为Fill(如果您希望图片填写表单)
  • 将图片框的SizeMode设置为StretchImage

最后:

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)
    {                     
        PictureBox1.Image = Image.FromFile(dlg.Filename);
    }
    dlg.Dispose();
}

答案 3 :(得分:2)

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();
    if (open.ShowDialog() == DialogResult.OK)
        pictureBox1.Image = Bitmap.FromFile(open.FileName);
}

答案 4 :(得分:1)

您也可以尝试PictureBox1.Image = Image.FromFile("<your ImagePath>" or <Dialog box result>);

答案 5 :(得分:1)

PictureBox.Image是一个属性,而不是一个方法。您可以这样设置:

PictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);

答案 6 :(得分:1)

您可以尝试以下操作:

private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog fDialog = new OpenFileDialog();
        fDialog.Title = "Select file to be upload";
        fDialog.Filter = "All Files|*.*";
        //  fDialog.Filter = "PDF Files|*.pdf";
        if (fDialog.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = fDialog.FileName.ToString();
        }
    }

答案 7 :(得分:0)

这很简单。只需添加:

PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;