I wanted to offer a solution that I came up with:
public static class IconExtensions { [DllImport("gdi32.dll", SetLastError = true)] private static extern bool DeleteObject(IntPtr hObject); public static ImageSource ToImageSource(this Icon icon) { Bitmap bitmap = icon.ToBitmap(); IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (!DeleteObject(hBitmap)) { throw new Win32Exception(); } return wpfBitmap; } }
Then I have an IconToImageSourceConverter that just calls the method above.
So that I can add icons as images, I added:
<DataTemplate DataType="{x:Type drawing:Icon}"> <Image Source="{Binding Converter={converter:IconToImageSourceConverter}}" MaxWidth="{Binding Width}" MaxHeight="{Binding Height}"/> </DataTemplate>
Thus, if the icon is placed directly in XAML, if it still displays:
<x:Static MemberType="{x:Type drawing:SystemIcons}" Member="Asterisk"/>
Otherwise, the converter can be used in place, for example:
<Image Source="{Binding Source={x:Static drawing:SystemIcons.Asterisk}, Converter={converter:IconToImageSourceConverter}}"/>
source share