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

问题描述 投票:15回答:8

我需要使用打开文件对话框以窗口形式打开位图图像(我将从驱动器中加载它)。图像应适合图片框。这是我尝试过的一些代码,但出现错误!

 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();
        }
c# winforms bitmap picturebox openfiledialog
8个回答
32
投票

您必须使用从磁盘上的文件加载图像的Bitmap class创建Bitmap的实例。现在编写代码时,您正在尝试使用constructor overload property,就好像它是method

更改您的代码使其看起来像这样(还利用PictureBox.Image来确保正确处理,而不是手动调用using statement方法):

using

当然,这不会在表单上的任何地方display图像,因为您创建的图片框控件尚未添加到表单中。您需要使用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); } } } 中。注意此处添加到上述代码中的行:

Controls collection

8
投票

工作正常。试试这个,

Controls

6
投票

您应该尝试:

  • 以可视化的形式创建图片框(更容易)
  • 将图片框的Add method属性设置为Add(如果要填充图像)
  • 将图片框的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); } } } 设置为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; } }

最后:

Dock

2
投票
Fill

1
投票

[您也可以这样尝试,SizeMode


1
投票

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

StretchImage

1
投票

您可以尝试以下操作:

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

0
投票

很简单。只需添加:

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();
    if (open.ShowDialog() == DialogResult.OK)
        pictureBox1.Image = Bitmap.FromFile(open.FileName);
}
© www.soinside.com 2019 - 2024. All rights reserved.