C# .exe和.dll檔案圖示資源提取工具
阿新 • • 發佈:2021-06-23
Windows 可執行檔案(.exe)和動態庫檔案(.dll)圖示資源提取工具
功能
- 圖示資源預覽
- 圖示資源匯出(僅支援匯出 PNG 格式)
程式碼
獲取圖示資源使用了 Win32 API PrivateExtractIconsW
PrivateExtractIconsW 對應的 C# 程式碼
[DllImport("User32.dll")] internal static extern uint PrivateExtractIcons( /* _In_reads_(MAX_PATH) */ string szFileName, /* _In_ */ int nIconIndex, /* _In_ */ int cxIcon, /* _In_ */ int cyIcon, /* _Out_writes_opt_(nIcons) */ IntPtr[] phicon, /* _Out_writes_opt_(nIcons) */ uint[] piconid, /* _In_ */ uint nIcons, /* _In_ */ uint flags);
引數:
szFileName 要從中提取圖示的檔案的路徑和名稱。
nIconIndex 要提取的第一個圖示的從零開始的索引。例如,如果此值為零,則該函式會提取指定檔案中的第一個圖示。
cxIcon 想要的水平圖示大小。
cyIcon 想要的垂直圖示大小。
phicon 指向返回的圖示控制代碼陣列的指標。
piconid 指向最適合當前顯示裝置的圖示的返回資源識別符號的指標。
nIcons 要從檔案中提取的圖示數。此引數僅在從 .exe 和 .dll 檔案中提取時有效。
flags 指定控制此功能的標誌。
主要步驟
- 需要先獲取檔案中的圖示總數量。phicon 引數為 NULL,返回值為檔案中的圖示數
int _nIcons = PrivateExtractIcons(filePath, 0, 0, 0, null, null, 0, 0);
- 獲取所有圖示資源控制代碼。返回值為獲取到的圖示數量
IntPtr[] phicon = new IntPtr[_nIcons];
uint[] piconid = new uint[_nIcons];
uint nIcons = PrivateExtractIcons(filePath, 0, 32, 32, phicon, piconid, _nIcons, 0);
- 轉換成點陣圖。需要釋放資源
for (int i = 0; i < nIcons; i++) { Icon icon = Icon.FromHandle(phicon[i]); Bitmap bitmap = icon.ToBitmap(); icon.Dispose(); _ = DestroyIcon(phicon[i]); }