Load a bitmap image into Windows Forms using open file dialog

后端 未结 8 758
梦毁少年i
梦毁少年i 2020-12-10 02:21

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 the code I t

8条回答
  •  情书的邮戳
    2020-12-10 02:46

    You should try to:

    • Create the picturebox visually in form (it's easier)
    • Set Dock property of picturebox to Fill (if you want image to fill form)
    • Set SizeMode of picturebox to StretchImage

    Finally:

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

提交回复
热议问题