加载一个位图图像到Windows窗体使用的打开文件对话框!位图、窗体、对话框、图像

2023-09-02 10:52:03 作者:你的倾城一笑抵我半壁江山

我需要用打开文件对话框窗口的形式打开的位图图像(我会从驱动器加载它).the形象应该适合在画面box.here一些$ C $词都尝试,但遇到错误!

 私人无效的button1_Click(对象发件人,EventArgs的)
        {
            打开文件对话框DLG =新的OpenFileDialog();

            dlg.Title =打开图片;
            dlg.Filter =BMP文件(* .BMP)| * .BMP;

            如果(dlg.ShowDialog()== DialogResult.OK)
            {
            图片框PictureBox1 =新的PictureBox();
                PictureBox1.Image(dlg.FileName);
            }

            dlg.Dispose();
        }
 

解决方案

您必须创建的 位图类,使用的构造函数重载加载图像从磁盘上的文件。当你的code现在写的,你要使用 PictureBox.Image 属性作为好像它是一个方式

更改code看起来像这样(也趁着 使用语句正确进行处理,而不是手动调用处置法):

 私人无效的button1_Click(对象发件人,EventArgs的)
{
    //创建包装的打开文件对话框实例在using语句,
    //而不是手动调用Dispose方法,以确保妥善处置
    使用(打开文件对话框DLG =新的OpenFileDialog())
    {
        dlg.Title =打开图片;
        dlg.Filter =BMP文件(* .BMP)| * .BMP;

        如果(dlg.ShowDialog()== DialogResult.OK)
        {
            图片框PictureBox1 =新的PictureBox();

            //从磁盘上的图片文件创建一个新的位图对象,
            //并分配给PictureBox.Image财产
            PictureBox1.Image =新位图(dlg.FileName);
        }
    }
}
 
新建一个工程,按下载图片 创建界面,完成后上传窗体文件 .frm文件 ,要求

当然,

,这不会给的显示的图像表单上的任意位置,因为你已经创建的图片框控件没有被添加到窗体。您需要添加您刚才创建的窗体的Controls集合使用Add法。注意补充上述code此行:

 私人无效的button1_Click(对象发件人,EventArgs的)
{
    使用(打开文件对话框DLG =新的OpenFileDialog())
    {
        dlg.Title =打开图片;
        dlg.Filter =BMP文件(* .BMP)| * .BMP;

        如果(dlg.ShowDialog()== DialogResult.OK)
        {
            图片框PictureBox1 =新的PictureBox();
            PictureBox1.Image =新位图(dlg.FileName);

            //添加新的控制到其父的控件集合
            this.Controls.Add(PictureBox1);
        }
    }
}
 

I need to open the bitmap image in the window form using open file dialog (i will load it from drive).the image should fit in the picture box.here is some code i have tried but got error!

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

解决方案

You have to create an instance of the Bitmap class, using the constructor overload that loads an image from a file on disk. As your code is written now, you're trying to use the PictureBox.Image property as if it were a method.

Change your code to look like this (also taking advantage of the using statement to ensure proper disposal, rather than manually calling the Dispose method):

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

Of course, that's not going to display the image anywhere on your form because the picture box control that you've created hasn't been added to the form. You need to add the new picture box control that you've just created to the form's Controls collection using the Add method. Note the line added to the above code here:

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

 
精彩推荐
图片推荐