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 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();
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
raghu
  • 431
  • 2
  • 11
  • 23
  • 1
    Which error did you get? – hemp May 25 '11 at 10:28
  • You should also wrap your OpenFileDialog in a using statement to dispose of it. It guarantees that the dialog is disposed properly in case you run into an error during its execution, where your manual dispose won't get executed. – Bob G May 25 '11 at 10:31
  • 'WindowsFormsApplication16.Form1' does not contain a definition for 'PictureBox1' and no extension method 'PictureBox1' accepting a first argument of type 'WindowsFormsApplication16.Form1' could be found (are you missing a using directive or an assembly reference?) – raghu May 25 '11 at 10:39
  • @stuart..where we should create a object named picturebox1 then,to use load() im getting the error(mentioned in above comment) – raghu May 25 '11 at 10:40

8 Answers8

40

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);
        }
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • gray...everything went fine,the window opened but couldnot open the image!!the bmp image i loaded is 49.6 MB is there any problem with size!in the designer window i just put a button(to load image) and picture box control. – raghu May 25 '11 at 12:07
15

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;

    }
}
Zero
  • 357
  • 2
  • 5
  • 20
6

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();
}
Marco
  • 56,740
  • 14
  • 129
  • 152
  • @marco..: everything went fine,the window opened but couldnot open the image!!the bmp image i loaded is 49.6 MB is there any problem with size!in the designer window i just put a button(to load image) and picture box control. – raghu May 25 '11 at 12:08
2
private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();
    if (open.ShowDialog() == DialogResult.OK)
        pictureBox1.Image = Bitmap.FromFile(open.FileName);
}
arserbin3
  • 6,010
  • 8
  • 36
  • 52
tanjila
  • 21
  • 1
1

You can try the following:

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();
        }
    }
Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
sukhdeep
  • 11
  • 1
1

You, can also try like this, PictureBox1.Image = Image.FromFile("<your ImagePath>" or <Dialog box result>);

1

PictureBox.Image is a property, not a method. You can set it like this:

PictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
hemp
  • 5,602
  • 29
  • 43
0

It's simple. Just add:

PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
Laurel
  • 5,965
  • 14
  • 31
  • 57