问题
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!
回答1:
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;
}
}
回答2:
Did you try reading the manual?
OpenFileDialog dlg = new OpenFileDialog();
// Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations
// OR Office Files
// OR All Files
dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt"
. You really should be looking for such trivial info on MSDN or even Gooogle, instead of Stack Overflow. MSDN is your friend, THE programming bible for .Net developers.
来源:https://stackoverflow.com/questions/3221287/using-the-openfiledialog-control-in-a-c-sharp-application