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

rag*_*ghu 15 .net c# bitmap picturebox winforms

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

 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();
        }
Run Code Online (Sandbox Code Playgroud)

Cod*_*ray 32

您必须使用构造函数重载创建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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,这不会在表单上的任何位置显示图像,因为您创建的图片框控件尚未添加到表单中.您需要使用该方法将刚刚创建的新图片框控件添加到表单的Controls集合中.注意这里添加到上面代码的行: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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Zer*_*ero 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;

    }
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*rco 6

你应该尝试:

  • 以形式直观地创建图片框(更容易)
  • 将图片Dock框的属性设置为Fill(如果要图像填写表格)
  • 一套SizeModepicturebox到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();
}
Run Code Online (Sandbox Code Playgroud)