Solved

Selecting an Image

Posted on 2006-10-23
3
200 Views
Last Modified: 2010-04-16
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
Comment
Question by:schenkp
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17792823
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
 

Author Comment

by:schenkp
ID: 17800232
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17800442
What version C#?

If C# 2005, set the SizeMode() property on the PictureBox accordingly.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now