Reputation: 981
I have a WPF application that needs to extract the icon off of an executable.
I found here that I can do this
Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);
but when I try to set the source of a WPF Image I get
"Cannot implicitly convert type 'System.Drawing.Icon' to 'System.Windows.Media.ImageSource'
Any suggestions ?
Upvotes: 22
Views: 30841
Reputation: 874
I use this extension method to convert from Icon to ImageSource:
public static System.Windows.Media.ImageSource ToWpfImageSource(this System.Drawing.Icon icon)
{
using (MemoryStream strm = new MemoryStream())
{
icon.Save(strm);
return System.Windows.Media.Imaging.BitmapFrame.Create(strm, System.Windows.Media.Imaging.BitmapCreateOptions.None, System.Windows.Media.Imaging.BitmapCacheOption.OnLoad);
}
}
...
Icon icon = Icon.ExtractAssociatedIcon(path);
ImageSouce imageWpf = icon.ToWpfImageSource();
Upvotes: 0
Reputation: 1883
System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon
method can be use to convert a System.Drawing.Icon
to wpf BitmapSource
.
using(Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName))
{
image.Source = Imaging.CreateBitmapSourceFromHIcon(ico.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
Upvotes: 17
Reputation: 665
I wanted to offer the solution I've come 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;
}
}
I then have a IconToImageSourceConverter that simply calls the method above.
To make it easy for me to add icons as images I also added this:
<DataTemplate DataType="{x:Type drawing:Icon}">
<Image Source="{Binding Converter={converter:IconToImageSourceConverter}}"
MaxWidth="{Binding Width}" MaxHeight="{Binding Height}"/>
</DataTemplate>
This way, if an icon is placed directly in XAML if will still be shown:
<x:Static MemberType="{x:Type drawing:SystemIcons}" Member="Asterisk"/>
Otherwise the converter can be used on location, like so:
<Image Source="{Binding Source={x:Static drawing:SystemIcons.Asterisk},
Converter={converter:IconToImageSourceConverter}}"/>
Upvotes: 13
Reputation: 338
I had a similar problem and in few steps we can get the image source:
ImageSource imageSource;
Icon icon = Icon.ExtractAssociatedIcon(path);
using (Bitmap bmp = icon.ToBitmap())
{
var stream = new MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
imageSource = BitmapFrame.Create(stream);
}
We can use this image source to feed the property source in out XAML:
<Image Source="{Binding Path=ImageSource, Mode=OneTime}" />
Upvotes: 5
Reputation: 942177
Icons get no love in the .NET framework. You'll have to use Icon.Save() to save the icon you got into a MemoryStream. Which allows you to use the IconBitmapDecoder constructor that takes a stream.
Upvotes: 11