如何在WPF中显示Windows文件图标?

25

目前我通过调用SHGetFileInfo获取本地图标,然后使用下面的代码将其转换为位图。最终该位图会在WPF表单中显示。

有没有更快的方法来完成同样的事情?

try
        {
            using (Icon i = Icon.FromHandle(shinfo.hIcon))
            {
                Bitmap bmp = i.ToBitmap();
                MemoryStream strm = new MemoryStream();
                bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
                BitmapImage bmpImage = new BitmapImage();
                bmpImage.BeginInit();
                strm.Seek(0, SeekOrigin.Begin);
                bmpImage.StreamSource = strm;
                bmpImage.EndInit();

                return bmpImage;
            }
        }
        finally
        {
            Win32.DestroyIcon(hImgLarge);
        }
5个回答

27

怎么样尝试这种方法:

var icon = System.Drawing.Icon.ExtractAssociatedIcon(fileName);
var bmp = icon.ToBitmap()

显然你还不够复杂 ;) - ocodo
6
@Slomojo使用System.Drawing和Bitmap map = Icon.ExtractAssociatedIcon(fileName).ToBitmap()也可以工作... (翻译:@Slomojo可以使用System.Drawing和Bitmap map = Icon.ExtractAssociatedIcon(fileName).ToBitmap()来实现相同的功能) - msfanboy
ToBitmap() 会丢失 alpha 通道。 - Prat
3
这仅提取32x32分辨率的图标。 - Basic
2
不要使用 icon.ToBitmap(),你可以直接将图标转换为 ImageSource,使用 icon.Handle 属性和 Thomas 或 Krysztof 的答案。或者参考 这里 - Dzyann

22
using System.Windows.Interop;

...

ImageSource img = Imaging.CreateBitmapSourceFromHIcon(
    shinfo.hIcon,
    new Int32Rect(0,0,i.Width, i.Height),
    BitmapSizeOptions.FromEmptyOptions());

11

结合Krzysztof Kowalczyk的回答和一些搜索,我想出了以下内容:

方法:

/*
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
*/

     public static ImageSource GetIcon(string strPath, bool bSmall)
        {
          Interop.SHFILEINFO info = new Interop.SHFILEINFO(true);
          int cbFileInfo = Marshal.SizeOf(info);
          Interop.SHGFI flags;
          if (bSmall)
            flags = Interop.SHGFI.Icon | Interop.SHGFI.SmallIcon | Interop.SHGFI.UseFileAttributes;
          else
            flags = Interop.SHGFI.Icon | Interop.SHGFI.LargeIcon | Interop.SHGFI.UseFileAttributes;

          Interop.SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);

          IntPtr iconHandle = info.hIcon;
          //if (IntPtr.Zero == iconHandle) // not needed, always return icon (blank)
          //  return DefaultImgSrc;
          ImageSource img = Imaging.CreateBitmapSourceFromHIcon(
                      iconHandle,
                      Int32Rect.Empty,
                      BitmapSizeOptions.FromEmptyOptions());
          Interop.DestroyIcon(iconHandle);
          return img;
        }

以及Interop类:

using System;
using System.Runtime.InteropServices;
public static class Interop
  {
    /// <summary>Maximal Length of unmanaged Windows-Path-strings</summary>
    private const int MAX_PATH = 260;
    /// <summary>Maximal Length of unmanaged Typename</summary>
    private const int MAX_TYPE = 80;


    [Flags]
    public enum SHGFI : int
    {
      /// <summary>get icon</summary>
      Icon = 0x000000100,
      /// <summary>get display name</summary>
      DisplayName = 0x000000200,
      /// <summary>get type name</summary>
      TypeName = 0x000000400,
      /// <summary>get attributes</summary>
      Attributes = 0x000000800,
      /// <summary>get icon location</summary>
      IconLocation = 0x000001000,
      /// <summary>return exe type</summary>
      ExeType = 0x000002000,
      /// <summary>get system icon index</summary>
      SysIconIndex = 0x000004000,
      /// <summary>put a link overlay on icon</summary>
      LinkOverlay = 0x000008000,
      /// <summary>show icon in selected state</summary>
      Selected = 0x000010000,
      /// <summary>get only specified attributes</summary>
      Attr_Specified = 0x000020000,
      /// <summary>get large icon</summary>
      LargeIcon = 0x000000000,
      /// <summary>get small icon</summary>
      SmallIcon = 0x000000001,
      /// <summary>get open icon</summary>
      OpenIcon = 0x000000002,
      /// <summary>get shell size icon</summary>
      ShellIconSize = 0x000000004,
      /// <summary>pszPath is a pidl</summary>
      PIDL = 0x000000008,
      /// <summary>use passed dwFileAttribute</summary>
      UseFileAttributes = 0x000000010,
      /// <summary>apply the appropriate overlays</summary>
      AddOverlays = 0x000000020,
      /// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>
      OverlayIndex = 0x000000040,
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct SHFILEINFO
    {
      public SHFILEINFO(bool b)
      {
        hIcon = IntPtr.Zero;
        iIcon = 0;
        dwAttributes = 0;
        szDisplayName = "";
        szTypeName = "";
      }
      public IntPtr hIcon;
      public int iIcon;
      public uint dwAttributes;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
      public string szDisplayName;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_TYPE)]
      public string szTypeName;
    };

    [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    public static extern int SHGetFileInfo(
      string pszPath,
      int dwFileAttributes,
      out    SHFILEINFO psfi,
      uint cbfileInfo,
      SHGFI uFlags);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool DestroyIcon(IntPtr hIcon);
  }

源代码


2
复制/粘贴解决方案!... +1。只是一个小注释:您应该坚持使用“BitmapSource”基类,而不是“ImageSource”,因为一旦升级,将更难还原到基类。否则完美的答案。谢谢。 - VeV
1
完美运行。一个改进:由于为许多文件加载图标可能需要一些时间,您可能希望在工作线程中执行此操作。要能够在用户界面中使用此类图像,它必须被冻结:'img.Freeze();'无论如何它都不会改变。 - ygoe
很棒的解决方案。我扩展了它,以支持目录。当我想要检索文件夹图标时,不再将魔数256传递给SHGetFileInfo,而是传递FILE_ATTRIBUTE_DIRECTORY( 0x10 ),否则传递FILE_ATTRIBUTE_NORMAL( 0x80 )。我在Interop类中添加了这两个值作为常量。使用了这里这里的信息。 - Nikolaos Georgiou

8

托马斯的代码可以进一步简化。以下是带有额外错误检查的完整代码:

            Interop.SHGetFileInfo(path, isFile, ref pifFileInfo);
            IntPtr iconHandle = pifFileInfo.hIcon;
            if (IntPtr.Zero == iconHandle)
                return DefaultImgSrc;
            ImageSource img = Imaging.CreateBitmapSourceFromHIcon(
                        iconHandle,
                        Int32Rect.Empty,
                        BitmapSizeOptions.FromEmptyOptions());
            User32.DestroyIcon(iconHandle);
            return img;

区别在于:
  • 无需创建Icon对象
  • 确保处理iconHandle为0(IntPtr.Zero)的情况,例如返回一些预定义的ImageSource对象
  • 如果来自SHGetFileInfo(),请确保使用win32 api DestroyIcon()

请问您能提供Interop代码吗?我在尝试将您的代码与Windows API中的SHGetFileInfo函数签名进行匹配时遇到了问题。我发现所有其他解决方案都使用System.Drawing,但它似乎与WPF完全不兼容,所以为什么要使用它呢?这看起来是最直接的方法。如果我能让它工作的话。 - ygoe
Interop类和其他组合在我的回答中(太长了,无法在评论中显示)。 - Jan 'splite' K.

1

我相信这里有一种更简单(更易管理)的解决方法。

http://www.pchenry.com/Home/tabid/36/EntryID/193/Default.aspx

解决方案的关键在于这里。

System.Drawing.Icon formIcon = IconsInWPF.Properties.Resources.Habs;
MemoryStream stream = new MemoryStream();
formIcon.Save(stream);
this.Icon = BitmapFrame.Create(stream);

3
使用资源后记得进行处理。在这种情况下,我认为你的流没有关闭。 - Mathias Lykkegaard Lorenzen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接