Raj*_*123 3 c# wpf openfiledialog image-formats
我正在开发一个WPF应用程序,用户可以上传照片.我为文件扩展名编写了以下代码.
OpenFIleDialog.Filter = "JPEG Images|*.jpg|PNG Images|*.png|GIF Images|*.gif|BITMAPS|*.bmp|TIFF Images|*.tiff|TIFF Images|*.tif";
Run Code Online (Sandbox Code Playgroud)
以ms画面保存文件时,我们有如下选项

在这里我们可以看到相同的格式(.bmp和.dib)被用于4个选项.
我的问题是可以使用OpenFileDialog完成.如果是这样,怎么样?
它很简单,只需像这样添加你的过滤器
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 */
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到重复*.doc&*.docx.因此,根据所选值,您可以决定应用哪种编码(在您的情况下).