从文件扩展名中获取ImageFormat [英] Get ImageFormat from File Extension
问题描述
是否有快速方法将ImageFormat对象与特定文件扩展名相关联?我正在寻找比每种格式更快的字符串比较。
Is there quick way to get the ImageFormat object associated to a particular file extension? I'm looking for quicker than string comparisons for each format.
推荐答案
以下是我发现的一些旧代码应该可以解决的问题:
Here's some old code I found that should do the trick:
string InputSource = "mypic.png";
System.Drawing.Image imgInput = System.Drawing.Image.FromFile(InputSource);
Graphics gInput = Graphics.fromimage(imgInput);
Imaging.ImageFormat thisFormat = imgInput.RawFormat;
这需要实际打开并测试图像 - 文件扩展名被忽略。假设你正在打开文件,这比信任文件扩展名要强大得多。
This requires actually opening and testing the image--the file extension is ignored. Assuming you are opening the file anyway, this is much more robust than trusting a file extension.
如果你没有打开文件,就没有什么更快了(在一个性能感)而不是字符串比较 - 当然不会调用操作系统来获取文件扩展名映射。
If you aren't opening the files, there's nothing "quicker" (in a performance sense) than a string comparison--certainly not calling into the OS to get file extension mappings.
这篇关于从文件扩展名中获取ImageFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!