Wpf 如何在XAML中获取容器(如dll)中的特定图标?

Wpf 如何在XAML中获取容器(如dll)中的特定图标?,wpf,xaml,Wpf,Xaml,我可以在XAML中设置图标容器: <Image Source="Shell32.dll.ico" /> 但是如何在XAML中设置容器中的图标索引?比如: <Image Source="Shell32.dll,5" /> 或者说: <Image Source="Shell32.dll" Index="5" /> 等等。事情就是这样:首先是IValueConverter: using System; using System.Diagnostic

我可以在XAML中设置图标容器:

<Image Source="Shell32.dll.ico" />

但是如何在XAML中设置容器中的图标索引?比如:

<Image Source="Shell32.dll,5" />

或者说:

<Image Source="Shell32.dll" Index="5" />


等等。

事情就是这样:首先是
IValueConverter

using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

[ValueConversion(typeof(string), typeof(ImageSource))]
public class HabeasIcon : IValueConverter
{   
    [DllImport("shell32.dll")]
    private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string[] fileName = ((string)parameter).Split('|');

        if (targetType != typeof(ImageSource))
            return Binding.DoNothing;

        IntPtr hIcon = ExtractIcon(Process.GetCurrentProcess().Handle, fileName[0], int.Parse(fileName[1]));

        ImageSource ret = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        return ret;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    { throw new NotImplementedException(); }
}
XAML:

<Image Source="{Binding Converter={StaticResource iconExtractor}, ConverterParameter=c:\\Windows\\System32\\shell32.dll|72}"/>


您的第一个版本似乎不适合我。因为您可能没有名为“Shell32.dll.ico”的文件。。。这只是一个演示。我尝试将Shell32.dll复制到我的项目中,并将其重命名为Shell32.dll.ico。但Shell32.dll不是图标文件。这正是我的问题主题。。。无论如何,我通过定制的IValueConverter解决了这个问题。你能以答案的形式与我们分享你的解决方案吗?