Selecting an Image

Hi,
What is the easiest way to have a user click on a button and select an image from there C:\ to be put into a picture box or panel?

Thanks

Peter
schenkpAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Something like...

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Select an Image File";
            ofd.InitialDirectory = @"c:\";
            ofd.Filter = "Image Files|*.bmp;*.jpg";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    pictureBox1.Image = Image.FromFile(ofd.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ofd.FileName, "Error opening File");
                }
            }
        }
0
 
schenkpAuthor Commented:
Hi Idle Mind,
Do you know how to size the picture that the user is selecting so it fits within the picturebox?

Thanks

Peter
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version C#?

If C# 2005, set the SizeMode() property on the PictureBox accordingly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.