3

我正在开发一个 WPF 应用程序,用户可以在其中上传照片。我为文件扩展名编写了以下代码。

    OpenFIleDialog.Filter = "JPEG Images|*.jpg|PNG Images|*.png|GIF Images|*.gif|BITMAPS|*.bmp|TIFF Images|*.tiff|TIFF Images|*.tif";

在 ms paint 中保存文件时,我们有以下选项 在此处输入图像描述

在这里我们可以看到相同的格式(.bmp 和 .dib)被用于 4 个选项。

我的问题是这可以使用 OpenFileDialog 来完成吗?如果是这样,怎么做?

4

1 回答 1

5

它很简单,只需像这样添加您的过滤器

openFileDialog.Filter = "Office Files(Document or Excel)|*.doc;*.docx;*.xlsx;*.xls|Word Document(*.doc *.docx)|*.doc;*.docx";
var result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                var selectedFile = openFileDialog.FileName;
                var filterIndex = openFileDialog.FilterIndex;
                if(filterIndex == 1)
                { 
                   /* Code to perform if first filter (Office files in this case) is selected */ 
                }
                else if (filterIndex == 2)
                { 
                   /* Code to perform if second filter (Word Document in this case) is selected */
                }

在这里你可以看到 *.doc & *.docx 是重复的。因此,根据所选值,您可以决定应用哪种编码(在您的情况下)。

于 2014-02-05T07:41:41.067 回答