3
我發現有關獲取系統映像文件甚至文件擴展名的各種文章。我有以下方法正在爲獲得小16x16和大32x32圖像工作。如何使用Windows shell爲文件擴展名獲取大圖標?
// DLL Import
[DllImport("shell32")]
private static extern IntPtr SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
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 attributes
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 attributes
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 dwFileAttribute
SHGFI_ADDOVERLAYS = 0x000000020, // apply the appropriate overlays
SHGFI_OVERLAYINDEX = 0x000000040 // Get the index of the overlay
}
public static Icon GetSmallIconForExtension(string 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(string 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;
}
從上述方法之一得到的圖標後,我然後將其轉換爲位圖如下所示:
Image image = icon.ToBitmap();
上述指定SHGetFileInfoConstants.SHGFI_LARGEICON
當我得到32×32大小的位圖圖像。然而,在Windows資源管理器中,當您選擇查看Medium Icons
時,會獲得48x48大小的圖標。
我想要48x48大小的圖標(不是32x32)。我需要做什麼?
您是否試過SHGFI_SHELLICONSIZE?只發布真實的代碼。 – 2012-01-27 14:00:45
我也嘗試這樣的: 'IntPtr的PTR =的SHGetFileInfo( 延伸, FILE_ATTRIBUTE_NORMAL, REF shinfo,(UINT)Marshal.SizeOf(shinfo) (INT)(SHGetFileInfoConstants.SHGFI_ICON | SHGetFileInfoConstants.SHGFI_SYSICONINDEX | SHGetFileInfoConstants。 SHGFI_LARGEICON | SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES | SHGetFileInfoConstants.SHGFI_TYPENAME));' 我仍然只有32x32(順便說一句,我發佈的是我的實際代碼)。 – Elan 2012-01-27 14:29:53
這幾乎是重複的:http://stackoverflow.com/questions/6434178/get-large-icon-from-file-extension – arx 2012-01-27 14:53:22