C# OpenFileDialog扩展类似于MS Paint';保存文件对话框
我正在开发一个WPF应用程序,用户可以上传照片。我为文件扩展名编写了以下代码C# OpenFileDialog扩展类似于MS Paint';保存文件对话框,c#,wpf,openfiledialog,image-formats,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"; 在ms paint中保存文件时,我们有以下选项 在这里,我们可以看到相同的格式(.bmp&.dib)用于4个选项 我的问题是,这可以使用OpenFil
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完成吗。如果是这样,怎么做?很简单,只需像这样添加您的过滤器
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是重复的。因此,根据所选的值,您可以决定应用哪种编码(在您的情况下)。这会以不同的格式保存文件吗?我的意思是当使用mono | | | | 16色| | 256色| | 24位时,保存的文件中的变化很明显。扩展名始终为.bmp,但图片质量不同。openfiledialog如何知道是否所有格式都显示为.bmp(此处为.doc或.docx)。用户可以看到的字符串会有什么不同吗。@Raj123我添加了一些代码,您可以使用OpenFileDialog的FilterIndex属性来达到这个目的。OpenFileDialog只生成一个字符串,不多也不少。这取决于您确定真正的图像格式。这几乎从来都不是一个问题,像Image.FromFile()这样的方法可以从文件头自己计算出来,而不考虑文件扩展名。