[英]Display Icon in WPF Image
I have a WPF application that needs to extract the icon off of an executable.我有一个 WPF 应用程序需要从可执行文件中提取图标。
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但是当我尝试设置 WPF 图像的来源时,我得到
"Cannot implicitly convert type 'System.Drawing.Icon' to 'System.Windows.Media.ImageSource' “无法将类型‘System.Drawing.Icon’隐式转换为‘System.Windows.Media.ImageSource’
Any suggestions ?有什么建议 ?
System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon
method can be use to convert a System.Drawing.Icon
to wpf BitmapSource
. System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon
方法可用于将System.Drawing.Icon
转换为 wpf BitmapSource
。
using(Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName))
{
image.Source = Imaging.CreateBitmapSourceFromHIcon(ico.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
Icons get no love in the .NET framework.图标在 .NET 框架中不受欢迎。 You'll have to use Icon.Save() to save the icon you got into a MemoryStream.您必须使用 Icon.Save() 将您获得的图标保存到 MemoryStream 中。 Which allows you to use the IconBitmapDecoder constructor that takes a stream.它允许您使用采用流的 IconBitmapDecoder 构造函数。
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.然后我有一个 IconToImageSourceConverter ,它只是调用上面的方法。
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:这样,如果在 XAML 中直接放置一个图标 if 仍然会显示:
<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}}"/>
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:我们可以使用此图像源在 XAML 中提供属性源:
<Image Source="{Binding Path=ImageSource, Mode=OneTime}" />
I use this extension method to convert from Icon to ImageSource:我使用这个扩展方法从 Icon 转换为 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.