Search code examples
c#.netopenfiledialog

Using the OpenFileDialog control in a C# application


I'm sure I've asked this question before but searching does nothing and I completely forgot how to do this.

I need a way to have a user choose a picture from their hard drive and load that picture to an Image class using the location.

I've done this in the past, but as I said I can't remember how I did it.

I know you can apply a file type filter to the OpenFileDialog.

private void LoadImageToMemory()
        {
            openFileDialog1.Filter = "JPEG | jpeg";
            openFileDialog1.ShowDialog();            
        }

Any guidance? Thank you!


Solution

  • I figured it out!

    In case anyone has the same question, this is how you do it.

    private void LoadImageToMemory()
            {
                openFileDialog1.Filter = "png files (*.png)|*.png|jpg files (*.jpg)|*.jpg";
                openFileDialog1.Multiselect = false;
                openFileDialog1.InitialDirectory = @"C:\";
                openFileDialog1.Title = "Select a picture to transform.";
    
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    txtFileName.Text = openFileDialog1.FileName;
                }            
            }