Get supported image formats from BitmapImage

谁说我不能喝 提交于 2019-12-05 14:05:00

If you do not want to deal with WIC directly as shown in the source code linked to in the answer mentioned by Clemens, you can read a list of additional codecs (those that are not supported by WIC by default) with their names and supported file extensions directly from the registry.

See the following sample code.

/// <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;
}

Note that this can return different results depending on whether you are running in a 32 bit or 64 bit process. For example, on my Windows 10 machine I have a Photoshop decoder by Microsoft installed to read psd files. However, only a 32 bit version is installed.

So, when I try to load a Photoshop psd file via BitmapImage, this succeeds when running a 32 bit application but not when running a 64 bit application. The code above reading the installed decoders from the registry reflects this correctly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!