Using the OpenFileDialog control in a C# application

血红的双手。 提交于 2020-01-07 05:12:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!