得到的BitmapImage支持的图像格式 [英] Get supported image formats from BitmapImage

查看:344
本文介绍了得到的BitmapImage支持的图像格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得通过System.Windows.Media.Imaging.BitmapImage支持的图像格式列表?

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

I am writing a simple image processing tool in C# WPF. The BitmapImage class is one of the more useful bitmap classes as it is able to decode from a wide variety of formats.

在特别的,它能够在我的电脑上打开NEF(尼康RAW格式)。很可能的BitmapImage可以开各种各样的来自其他制造商的RAW格式,功能我热衷于利用。

In particular, it is able to open NEF (Nikon's RAW format) on my computer. It is likely that BitmapImage can open a wide variety of RAW formats from other manufacturers, a function I am keen to make use of.

由于我不知道每一个可以打开一个BitmapImage的格式,我目前使用try / catch语句,试图从每个文件建立一个BitmapImage的是,用户试图打开。这显然不是最有效的方法。

As I don't know every format that can be opened as a BitmapImage, I am currently using a try/catch to try and construct a BitmapImage from every file that the user tries to open. This is clearly not the most efficient way.

据我所知,从的BitmapImage的BitmapSource,它决定的哪些文件可以通过查看用户的可用的编解码器。很可能因此该编解码器的可用性机器之间变化,意味着支持的格式列表不能硬编码到程序中。我需要一种方法来检查内容的用户的机器上,这些支持的格式是。

As far as I know, BitmapImage inherits from BitmapSource, which decides which files it can open by looking on the user's system for available codecs. It's likely therefore that codec availability varies between machines, meaning a list of supported formats can't be hard-coded into the program. I need a way to check what these supported formats on a user's machine are.

我发现的这个方法在System.Drawing中。这将返回所支持的编解码器的列表,支持的文件扩展名列表,以及Systems.Windows.Media.Imaging等效会正是我需要的。

I found this method in System.Drawing. This returns a list of supported codecs with a list of supported file extensions, and an equivalent for Systems.Windows.Media.Imaging would be exactly what I need.

推荐答案

如果你不想处理WIC直接作为在由克莱门斯说对链接到源代码所示,你可以阅读更多的编解码器列表(那些不WIC受支持默认情况下)与他们从注册表名称和支持的文件扩展名直接。

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

请注意,这可以返回取决于您是否在32运行的不同结果位或64位的过程。例如,我的Windows 10的机器上我有一个Photoshop解码器通过安装微软阅读PSD文件。然而,只有32位版本的安装。

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.

所以,当我尝试加载通过的BitmapImage 一个Photoshop PSD文件,这个成功运行32位应用程序时但不能运行64位应用程序时。上述从注册表中读取已安装的解码器的代码反映了这个正确的。

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.

这篇关于得到的BitmapImage支持的图像格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆