Comment obtenir de grandes icons pour une extension de fichier à l’aide du shell Windows?

J’ai trouvé divers articles sur l’obtention des images système pour un fichier ou même une extension de fichier. J’ai les méthodes belows qui fonctionnent pour obtenir de petites images 16×16 et 32×32.

// DLL Import [DllImport("shell32")] private static extern IntPtr SHGetFileInfo( ssortingng pszPath, uint dwFileAtsortingbutes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags); // Constants/Enums private const int FILE_ATTRIBUTE_NORMAL = 0x80; private enum SHGetFileInfoConstants : int { SHGFI_ICON = 0x100, // get icon SHGFI_DISPLAYNAME = 0x200, // get display name SHGFI_TYPENAME = 0x400, // get type name SHGFI_ATTRIBUTES = 0x800, // get atsortingbutes SHGFI_ICONLOCATION = 0x1000, // get icon location SHGFI_EXETYPE = 0x2000, // return exe type SHGFI_SYSICONINDEX = 0x4000, // get system icon index SHGFI_LINKOVERLAY = 0x8000, // put a link overlay on icon SHGFI_SELECTED = 0x10000, // show icon in selected state SHGFI_ATTR_SPECIFIED = 0x20000, // get only specified atsortingbutes SHGFI_LARGEICON = 0x0, // get large icon SHGFI_SMALLICON = 0x1, // get small icon SHGFI_OPENICON = 0x2, // get open icon SHGFI_SHELLICONSIZE = 0x4, // get shell size icon SHGFI_PIDL = 0x8, // pszPath is a pidl SHGFI_USEFILEATTRIBUTES = 0x10, // use passed dwFileAtsortingbute SHGFI_ADDOVERLAYS = 0x000000020, // apply the appropriate overlays SHGFI_OVERLAYINDEX = 0x000000040 // Get the index of the overlay } public static Icon GetSmallIconForExtension(ssortingng extension) { // Get the small icon and clone it, as we MUST destroy the handle when we are done. SHFILEINFO shinfo = new SHFILEINFO(); IntPtr ptr = SHGetFileInfo( extension, FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)(SHGetFileInfoConstants.SHGFI_ICON | SHGetFileInfoConstants.SHGFI_SMALLICON | SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES | SHGetFileInfoConstants.SHGFI_TYPENAME)); Icon icon = Icon.FromHandle(shinfo.hIcon).Clone() as Icon; DestroyIcon(shinfo.hIcon); return icon; } public static Icon GetLargeIconForExtension(ssortingng extension) { // Get the small icon and clone it, as we MUST destroy the handle when we are done. SHFILEINFO shinfo = new SHFILEINFO(); IntPtr ptr = SHGetFileInfo( extension, FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)( SHGetFileInfoConstants.SHGFI_ICON | SHGetFileInfoConstants.SHGFI_LARGEICON | SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES | SHGetFileInfoConstants.SHGFI_TYPENAME )); Icon icon = Icon.FromHandle(shinfo.hIcon).Clone() as Icon; DestroyIcon(shinfo.hIcon); return icon; } 

Après avoir obtenu l’icône de l’une des méthodes ci-dessus, je la convertis ensuite en bitmap comme suit:

 Image image = icon.ToBitmap(); 

Je reçois des images bitmap de taille 32×32 lorsque SHGetFileInfoConstants.SHGFI_LARGEICON spécifie SHGetFileInfoConstants.SHGFI_LARGEICON ci-dessus. Pourtant, dans l’Explorateur Windows, vous obtenez des icons de taille 48×48 lorsque vous choisissez d’afficher des icons de taille Medium Icons .

Je voudrais les icons de taille 48×48 (pas 32×32). Qu’est-ce que je dois faire?

Les tailles des icons du shell sont indiquées dans la documentation de SHGetImageList :

  • 16×16 = petit
  • 32×32 = grand
  • 48×48 = extralarge
  • 256×256 = jumbo

Il est donc normal que lorsque vous demandez les “grandes” icons, vous obtenez 32×32. Si vous voulez l’icône extralarge, vous devez les obtenir dans la liste d’images SHIL_EXTRALARGE .