Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get specific icon in a container (like dll) in XAML?

Tags:

wpf

xaml

I can set in XAML the icon container:

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

But how can I set in XAML the icon index in the container ? something like:

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

Or like:

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

etc...

like image 230
Tar Avatar asked Jul 02 '11 17:07

Tar


People also ask

What is URI in WPF?

In Windows Presentation Foundation (WPF), uniform resource identifiers (URIs) are used to identify and load files in many ways, including the following: Specifying the user interface (UI) to show when an application first starts. Loading images.


1 Answers

This is how it goes: first the 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(); }
}

The XAML:

<Image Source="{Binding Converter={StaticResource iconExtractor}, ConverterParameter=c:\\Windows\\System32\\shell32.dll|72}"/>
like image 131
Tar Avatar answered Oct 05 '22 06:10

Tar