从BitmapImage获取支持的图像格式

5

如何获取System.Windows.Media.Imaging.BitmapImage支持的图像格式列表?

我正在使用C# WPF编写一个简单的图像处理工具。BitmapImage类是比较有用的位图类之一,因为它能够解码各种格式的图像。

特别地,它能够在我的电脑上打开NEF(尼康的RAW格式)。很可能BitmapImage也能够打开其他制造商的各种RAW格式,这是我想要利用的功能。

由于我不知道BitmapImage能够打开哪些格式,目前我正在使用try/catch来尝试从用户尝试打开的每个文件构建BitmapImage。这显然不是最有效的方法。

据我所知,BitmapImage继承自BitmapSource,后者通过查找用户系统中可用的编解码器来决定可以打开哪些文件。因此,编解码器的可用性在不同的机器上可能会有所不同,这意味着支持的格式列表不能硬编码到程序中。我需要一种方法来检查用户机器上支持的这些格式。

我在 System.Drawing 中找到了 这种方法。它返回一个支持的编解码器列表和相应的文件扩展名列表,而 Systems.Windows.Media.Imaging 中类似的功能正是我所需的。

2
这个答案可能会很有趣。 - Clemens
1个回答

9
如果您不想像Clemens提到的答案中链接的源代码那样直接处理WIC,您可以直接从注册表中读取附加编解码器(默认情况下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机器上,我安装了由Microsoft提供的Photoshop解码器以读取psd文件。但是,只安装了32位版本。因此,当我尝试通过BitmapImage加载Photoshop psd文件时,如果运行32位应用程序,则成功,但在运行64位应用程序时则失败。上面的代码从注册表中读取已安装的解码器,正确反映了这一点。

你从哪里得到了微软的 Photoshop 解码器 - Daniel
@Daniel 抱歉,我不记得了。 - NineBerry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接