将过滤器设置为 OpenFileDialog 以允许典型的图像格式?
我有这段代码,如何让它接受所有典型的图像格式? PNG、JPEG、JPG、GIF?
这是我到目前为止所得到的:
public void EncryptFile()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.InitialDirectory = @"C:\";
dialog.Title = "Please select an image file to encrypt.";
if (dialog.ShowDialog() == DialogResult.OK)
{
//Encrypt the selected file. I'll do this later. :)
}
}
请注意,过滤器设置为 .txt 文件。我可以更改为 PNG,但其他类型又如何呢?
I have this code, how can I allow it to accept all typical image formats? PNG, JPEG, JPG, GIF?
Here's what I have so far:
public void EncryptFile()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.InitialDirectory = @"C:\";
dialog.Title = "Please select an image file to encrypt.";
if (dialog.ShowDialog() == DialogResult.OK)
{
//Encrypt the selected file. I'll do this later. :)
}
}
Notice that the filter is set to .txt files. I could change to PNG, but what of the other types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
来自文档,您可以使用的过滤器语法需要如下:
即用分号分隔多个扩展名 - 因此,
Image Files|*.jpg;*.jpeg;*.png;...
。From the docs, the filter syntax that you need is as follows:
i.e. separate the multiple extensions with a semicolon -- thus,
Image Files|*.jpg;*.jpeg;*.png;...
.下面是 ImageCodecInfo 建议的示例(在 VB 中):
它看起来像这样:
Here's an example of the ImageCodecInfo suggestion (in VB):
And it looks like this:
C# 的完整解决方案在这里:
Complete solution in C# is here:
要过滤图像文件,请使用此代码示例。
To filter images files, use this code sample.
我最喜欢汤姆·浮士德的回答。这是他的解决方案的 C# 版本,但稍微简化了一些。
I like Tom Faust's answer the best. Here's a C# version of his solution, but simplifying things a bit.
对于图像,您可以从 GDI (System.Drawing) 获取可用的编解码器,并通过一些工作从中构建您的列表。这将是最灵活的方式。
For images, you could get the available codecs from GDI (System.Drawing) and build your list from that with a little work. This would be the most flexible way to go.
只是使用 string.Join 和 LINQ 的死注释。
Just a necrocomment for using string.Join and LINQ.
写出您的应用程序所需的类型。 (参考。FileDialog.Filter< /a>)。以下是所有最常见的图像格式。
例如,使用
设置
FilterIndex = 2
来预选“所有图片”。 不要设置openFileDialog.InitialDirectory
,除非你有充分的理由。Write out the types that your application wants. (ref. FileDialog.Filter). Here are all the most common image formats.
Example use
Set
FilterIndex = 2
to preselect 'All pictures'. Don't setopenFileDialog.InitialDirectory
unless you have a good reason.对于那些不想每次都记住语法的人来说,这里有一个简单的封装:
用法:
For those who don't want to remember the syntax everytime here is a simple encapsulation:
Usage:
为了匹配不同类别文件的列表,您可以使用如下过滤器:
In order to match a list of different categories of file, you can use the filter like this:
使用此方法返回从 ImageCodecInfo 构建的过滤器兼容字符串 支持当前系统理解的图像格式。
结果如下:
Use this method to return a Filter compatible string built from ImageCodecInfo supporting image formats the current system understands.
Will result as shown:
这是极端的,但我使用名为 FILE_TYPES 的 2 列数据库表构建了一个动态的数据库驱动的过滤器,其中字段名为 EXTENSION 和 DOCTYPE:
显然我有许多不同的类型和扩展,但我在本示例中对其进行了简化。这是我的函数:
应该产生一个如下所示的过滤器:
This is extreme, but I built a dynamic, database-driven filter using a 2 column database table named FILE_TYPES, with field names EXTENSION and DOCTYPE:
Obviously I had many different types and extensions, but I'm simplifying it for this example. Here is my function:
Should yield a filter that looks like this:
您必须包含所有图像类型扩展名并允许所有文件作为选项。
所有文件|.|所有图像|.jpg;.jpeg;.png;.gif;.tif;.bmp|JPEG 图像|.jpg|PNG 图像|.png";
You must include all image type extensions and allow for all files as an option.
All Files|.|All images|.jpg;.jpeg;.png;.gif;.tif;.bmp|JPEG Images|.jpg|PNG Images|.png";