Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a bitmap image into Windows Forms using open file dialog

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 tried:

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();
}
like image 437
raghu Avatar asked May 25 '11 10:05

raghu


People also ask

How do I import a picture into Windows form?

Draw a PictureBox control on a form. In the Properties window, select the Image property, then select the ellipsis button to display the Open dialog box. If you're looking for a specific file type (for example, . gif files), select it in the Files of type box.

Which Toolbox is used to include an image on window form?

To include an image on the form we use which toolbox? Explanation: We can include an image in the form using Picture box control, which is instantiated using picture box tool. As we include the picture box, the properties of it appears in the Properties Window.

What is OpenFileDialog Visual Studio?

OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System.


2 Answers

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);
        }
    }
}
like image 61
Cody Gray Avatar answered Sep 24 '22 06:09

Cody Gray


Works Fine. Try this,

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;

    }
}
like image 44
Zero Avatar answered Sep 22 '22 06:09

Zero