C#exe、dllのアイコンを取得し、256 x 256解像度の取得をサポート


ネット上で多くの文章を探したが、大きなアイコンを取得することに成功せず、最大32 x 32しか取得できなかった.最後に自分で関連windows apiを試して、やっと1つの利用可能なものを見つけました.
 
主に使用するC++のPrivateExtractIcons関数です.詳細は、「PrivateExtractIcons function」を参照してください.
この関数の原文には、[This function is not intended for general use.It may be altered or unavailable in subsequent versions of Windows.]
 
 1 UINT WINAPI PrivateExtractIcons(
 2   _In_      LPCTSTR lpszFile,
 3   _In_      int     nIconIndex,
 4   _In_      int     cxIcon,
 5   _In_      int     cyIcon,
 6   _Out_opt_ HICON   *phicon,
 7   _Out_opt_ UINT    *piconid,
 8   _In_      UINT    nIcons,
 9   _In_      UINT    flags
10 );

C#はDLL importを使用して参照されます.
 1 [DllImport("User32.dll")]
 2 public static extern int PrivateExtractIcons(
 3     string lpszFile, //      exe,dll,ico,cur,ani,bmp
 4     int nIconIndex,  //          
 5     int cxIcon,      //       x
 6     int cyIcon,      //       y
 7     IntPtr[] phicon, //          
 8     int[] piconid,   //         
 9     int nIcons,      //         ,       .exe   .dll    
10     int flags        //  ,  0   ,     LoadImage  
11 );

 
説明:取得したいアイコンサイズが256 x 256で、実際のリソースファイルのアイコンサイズが256 x 256未満であれば、存在する最高スケール率、色数が最も豊富なアイコンが取得され、引き伸ばされます.
 
具体的なコードは以下の通りです.
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Drawing;
 4 using System.Drawing.Imaging;
 5 using System.IO;
 6 using System.Linq;
 7 using System.Runtime.InteropServices;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 
11 namespace TryGetExeLargeIcon
12 {
13     class Program
14     {
15         [STAThread]
16         static void Main(string[] args)
17         {
18             //       
19             var opfd = new System.Windows.Forms.OpenFileDialog { Filter = "    |*.exe;*.dll" };
20             if (opfd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
21             var file = opfd.FileName;
22 
23             //          
24             const string folderToSave = "D:\\temp\\";
25             if (!Directory.Exists(folderToSave)) Directory.CreateDirectory(folderToSave);
26 
27             //          
28             var iconTotalCount = PrivateExtractIcons(file, 0, 0, 0, null, null, 0, 0);
29 
30             //            
31             IntPtr[] hIcons = new IntPtr[iconTotalCount];
32             //     id
33             int[] ids = new int[iconTotalCount];
34             //          
35             var successCount = PrivateExtractIcons(file, 0, 256, 256, hIcons, ids, iconTotalCount, 0);
36 
37             //       
38             for (var i = 0; i < successCount; i++)
39             {
40                 //
41                 if (hIcons[i] == IntPtr.Zero) continue;
42 
43                 using (var ico = Icon.FromHandle(hIcons[i]))
44                 {
45                     using (var myIcon = ico.ToBitmap())
46                     {
47                         myIcon.Save(folderToSave + ids[i].ToString("000") + ".png", ImageFormat.Png);
48                     }
49                 }
50                 //    
51                 DestroyIcon(hIcons[i]);
52             }
53         }
54 
55 
56         //details: https://msdn.microsoft.com/en-us/library/windows/desktop/ms648075(v=vs.85).aspx
57         //Creates an array of handles to icons that are extracted from a specified file.
58         //This function extracts from executable (.exe), DLL (.dll), icon (.ico), cursor (.cur), animated cursor (.ani), and bitmap (.bmp) files. 
59         //Extractions from Windows 3.x 16-bit executables (.exe or .dll) are also supported.
60         [DllImport("User32.dll")]
61         public static extern int PrivateExtractIcons(
62             string lpszFile, //file name
63             int nIconIndex,  //The zero-based index of the first icon to extract.
64             int cxIcon,      //The horizontal icon size wanted.
65             int cyIcon,      //The vertical icon size wanted.
66             IntPtr[] phicon, //(out) A pointer to the returned array of icon handles.
67             int[] piconid,   //(out) A pointer to a returned resource identifier.
68             int nIcons,      //The number of icons to extract from the file. Only valid when *.exe and *.dll
69             int flags        //Specifies flags that control this function.
70         );
71 
72         //details:https://msdn.microsoft.com/en-us/library/windows/desktop/ms648063(v=vs.85).aspx
73         //Destroys an icon and frees any memory the icon occupied.
74         [DllImport("User32.dll")]
75         public static extern bool DestroyIcon(
76             IntPtr hIcon //A handle to the icon to be destroyed. The icon must not be in use.
77         );
78     }
79 }

 
転載先:https://www.cnblogs.com/JmlSaul/p/7515885.html