c# – 从BitmapImage获取支持的图像格式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 从BitmapImage获取支持的图像格式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3219字,纯文字阅读大概需要5分钟。
内容图文
![c# – 从BitmapImage获取支持的图像格式](/upload/InfoBanner/zyjiaocheng/788/1154457e1ce443cfb8db1bd4557c5f84.jpg)
如何获取System.Windows.Media.Imaging.BitmapImage支持的图像格式列表?
我正在用C#WPF编写一个简单的图像处理工具. BitmapImage类是更有用的位图类之一,因为它能够从各种格式进行解码.
特别是,它能够在我的电脑上打开NEF(尼康的RAW格式). BitmapImage可能会打开来自其他制造商的各种RAW格式,这是我热衷于使用的功能.
由于我不知道每个可以作为BitmapImage打开的格式,我目前正在使用try / catch尝试从用户尝试打开的每个文件构造一个BitmapImage.这显然不是最有效的方式.
据我所知,BitmapImage继承自BitmapSource,后者决定which files it can open by looking on the user’s system for available codecs.因此,编解码器的可用性可能因机器而异,这意味着支持的格式列表无法硬编码到程序中.我需要一种方法来检查用户机器上支持的格式是什么.
我在System.Drawing中找到了this method.这将返回支持的编解码器列表,其中包含受支持的文件扩展名列表,而Systems.Windows.Media.Imaging的等效编辑器正是我需要的.
解决方法:
如果您不想直接处理WIC,如Clemens提到的答案中链接的源代码所示,您可以阅读其他编解码器列表(默认情况下WIC不支持的编解码器)及其名称和支持的文件直接来自注册表的扩展.
请参阅以下示例代码.
/// <summary>
/// Sample code: Show the additional registered decoders
/// </summary>
private void Button_Click(object sender, RoutedEventArgs e)
{
var additionalDecoders = GetAdditionalDecoders();
foreach(var additionalDecoder in additionalDecoders)
{
MessageBox.Show(additionalDecoder.FriendlyName + ":" + additionalDecoder.FileExtensions);
}
}
/// <summary>
/// GUID of the component registration group for WIC decoders
/// </summary>
private const string WICDecoderCategory = "{7ED96837-96F0-4812-B211-F13C24117ED3}";
/// <summary>
/// Represents information about a WIC decoder
/// </summary>
public struct DecoderInfo
{
public string FriendlyName;
public string FileExtensions;
}
/// <summary>
/// Gets a list of additionally registered WIC decoders
/// </summary>
/// <returns></returns>
public static IEnumerable<DecoderInfo> GetAdditionalDecoders()
{
var result = new List<DecoderInfo>();
string baseKeyPath;
// If we are a 32 bit process running on a 64 bit operating system,
// we find our config in Wow6432Node subkey
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
baseKeyPath = "Wow6432Node\\CLSID";
}
else
{
baseKeyPath = "CLSID";
}
RegistryKey baseKey = Registry.ClassesRoot.OpenSubKey(baseKeyPath, false);
if (baseKey != null)
{
var categoryKey = baseKey.OpenSubKey(WICDecoderCategory + "\\instance", false);
if (categoryKey != null)
{
// Read the guids of the registered decoders
var codecGuids = categoryKey.GetSubKeyNames();
foreach (var codecGuid in codecGuids)
{
// Read the properties of the single registered decoder
var codecKey = baseKey.OpenSubKey(codecGuid);
if (codecKey != null)
{
DecoderInfo decoderInfo = new DecoderInfo();
decoderInfo.FriendlyName = Convert.ToString(codecKey.GetValue("FriendlyName", ""));
decoderInfo.FileExtensions = Convert.ToString(codecKey.GetValue("FileExtensions", ""));
result.Add(decoderInfo);
}
}
}
}
return result;
}
请注意,这可能会返回不同的结果,具体取决于您是在32位还是64位进程中运行.例如,在我的Windows 10机器上,我安装了一个用于读取psd文件的Photoshop解码器.但是,只安装了32位版本.
因此,当我尝试通过BitmapImage加载Photoshop psd文件时,这在运行32位应用程序时成功,但在运行64位应用程序时则不成功.从注册表中读取已安装的解码器的上述代码正确地反映了这一点.
内容总结
以上是互联网集市为您收集整理的c# – 从BitmapImage获取支持的图像格式全部内容,希望文章能够帮你解决c# – 从BitmapImage获取支持的图像格式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。