[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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
0
schenkp
Asked:
schenkp
  • 2
1 Solution
 
Mike TomlinsonMiddle 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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now