在 WPF 图像中显示图标

发布于 2024-09-04 07:11:40 字数 444 浏览 3 评论 0原文

我有一个 WPF 应用程序需要从可执行文件中提取图标。

我发现这里我可以做到这一点

Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);

,但是当我尝试设置 WPF 图像的源时,我得到

“无法将类型“System.Drawing.Icon”隐式转换为“System.Windows.Media.ImageSource”

有什么建议吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤芳又自赏 2024-09-11 07:11:40

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());
}

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());
}
挽心 2024-09-11 07:11:40

我想提供我想出的解决方案:

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;
    }

}

然后我有一个 IconToImageSourceConverter,它只需调用上面的方法。

为了方便我将图标添加为图像,我还添加了以下内容:

<DataTemplate DataType="{x:Type drawing:Icon}">
    <Image Source="{Binding Converter={converter:IconToImageSourceConverter}}" 
        MaxWidth="{Binding Width}" MaxHeight="{Binding Height}"/>
</DataTemplate>

这样,如果图标直接放置在 XAML 中,仍然会显示:

<x:Static MemberType="{x:Type drawing:SystemIcons}" Member="Asterisk"/>

否则可以在位置使用转换器,如下所示:

<Image Source="{Binding Source={x:Static drawing:SystemIcons.Asterisk}, 
    Converter={converter:IconToImageSourceConverter}}"/>                

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}}"/>                
云仙小弟 2024-09-11 07:11:40

图标在 .NET 框架中不受青睐。您必须使用 Icon.Save() 将获得的图标保存到 MemoryStream 中。它允许您使用接受流的 IconBitmapDecoder 构造函数。

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.

猥︴琐丶欲为 2024-09-11 07:11:40

我遇到了类似的问题,只需几个步骤我们就可以获得图像源:

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);
}

我们可以使用此图像源将属性源输入到 XAML 中:

 <Image Source="{Binding Path=ImageSource, Mode=OneTime}" />

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}" />
ま柒月 2024-09-11 07:11:40

我使用这个扩展方法从 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文