Search code examples
c#bitmappictureboxopenfiledialog

Open a Image File into a Picturebox making it a Bitmap


I want to be able to open either a .jpg, .gif or .bmp into a picturebox and make it a bitmap so i can draw on it, but at the moment it comes up with an error of 'Parameter is not valid' when i run my program and then try to load one of these image files into my picturebox. This is my current code:

openFileDialog1.InitialDirectory = @"N:\My Documents\My Pictures";
openFileDialog1.Filter = "JPEG Compressed Image (*.jpg|*.jpg" + "|GIF Image(*.gif|*.gif" + "|Bitmap Image(*.bmp|*.bmp";
openFileDialog1.Multiselect = true;
openFileDialog1.FilterIndex = 1;         
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    capturebox.BackgroundImage = new Bitmap(ofd2.FileName);
}

I don't know why this occurs, please help.

Thanks


Solution

  • You want to replace ofd2 with openFileDialog1 on the penultimate line.