代码之家  ›  专栏  ›  技术社区  ›  Sergio Tapia

在C应用程序中使用OpenFileDialog控件

  •  0
  • Sergio Tapia  · 技术社区  · 14 年前

    我肯定我以前问过这个问题,但是搜索什么也没做,我完全忘了怎么做。

    我需要一种方法让用户从硬盘中选择一张图片,并使用该位置将该图片加载到一个图像类中。

    我以前做过,但正如我所说,我记不起我是怎么做的。

    我知道您可以将文件类型过滤器应用到OpenFileDialog。

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

    有什么指导吗?谢谢您!

    2 回复  |  直到 14 年前
        1
  •  2
  •   Sergio Tapia    14 年前

    我明白了!

    如果有人有同样的问题,这就是你的做法。

    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
  •  0
  •   Saajid Ismail    14 年前

    你试过吗? 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"
    

    . 您真的应该在msdn甚至google上寻找这些琐碎的信息,而不是堆栈溢出。msdn是你的朋友,.net开发人员的编程圣经。