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

21

我希望使用打开文件对话框在窗体中打开位图图像(我将它从驱动器加载)。该图像应适合于图片框。

这是我尝试的代码:

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

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

    if (dialog.ShowDialog() == DialogResult.OK)
    {                     
        var PictureBox1 = new PictureBox();                    
        PictureBox1.Image(dialog.FileName);
    }

    dialog.Dispose();
}

1
你收到了哪个错误? - hemp
你还应该将 OpenFileDialog 包装在 using 语句中以进行处理。这可以确保在执行过程中遇到错误时,对话框会被正确地处理,而手动处理则无法执行。 - Bob G
'WindowsFormsApplication16.Form1' 不包含 'PictureBox1' 的定义,也没有接受类型为 'WindowsFormsApplication16.Form1' 的第一个参数的扩展方法 'PictureBox1' 可用(是否缺少 using 指令或程序集引用?) - raghu
@stuart..我们应该创建一个名为picturebox1的对象,然后使用load()函数时我遇到了错误(如上评论中所述)。 - raghu
8个回答

40
你需要创建一个Bitmap的实例,使用构造函数重载从磁盘上的文件中加载图像。根据你现在的代码,你试图像使用方法一样使用PictureBox.Image 属性
将你的代码更改为以下内容(同时利用using语句以确保正确处理,而不是手动调用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将新创建的图片框控件添加到表单的Controls collection中。请注意,在上面的代码中添加了以下行:
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);
        }
    }
}

灰色...一切都很顺利,窗口打开了,但无法打开图像!!我加载的bmp图像大小为49.6 MB,大小有问题吗!在设计师窗口中,我只放了一个按钮(用于加载图像)和图片框控件。 - raghu

15

运作良好。 试试这个,

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;

    }
}

6

您应该尝试以下操作:

  • 在表单中视觉上创建picturebox(这样更容易)
  • 将picturebox的属性设置为(如果要使图像填充表单)
  • 将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)
    {                     
        PictureBox1.Image = Image.FromFile(dlg.Filename);
    }
    dlg.Dispose();
}

@marco..:一切都很顺利,窗口打开了,但无法打开图像!!我加载的bmp图像大小为49.6 MB,大小有问题吗!在设计器窗口中,我只放了一个按钮(用于加载图像)和图片框控件。 - raghu

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

1

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

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

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

1
你也可以尝试这样做:PictureBox1.Image = Image.FromFile("<你的图片路径>" 或 <对话框结果>);。这与编程有关。

0

很简单。只需添加:

PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接