public static ImageSource GetIcon(IntPtr pIDL, int iconIndex, IconSize size = IconSize.Small) { ImageSource imageSource = null; if (IconsDictionary.TryGetValue(iconIndex, out imageSource)) { return(imageSource); } Int32Rect sizeRect; WinAPI.SHGFI flags; if (IconSize.Small == size) { flags = commonFlags | WinAPI.SHGFI.SHGFI_SMALLICON; sizeRect = new Int32Rect(0, 0, 16, 16); } else { flags = commonFlags | WinAPI.SHGFI.SHGFI_LARGEICON; sizeRect = new Int32Rect(0, 0, 32, 32); } WinAPI.SHFILEINFO shfileinfo = new WinAPI.SHFILEINFO(); WinAPI.SHGetFileInfo(pIDL, 256, out shfileinfo, (uint)Marshal.SizeOf(shfileinfo), WinAPI.SHGFI.SHGFI_SYSICONINDEX | WinAPI.SHGFI.SHGFI_PIDL | flags); imageSource = Imaging.CreateBitmapSourceFromHIcon(shfileinfo.hIcon, sizeRect, BitmapSizeOptions.FromEmptyOptions()); IconsDictionary.Add(iconIndex, imageSource); WinAPI.DestroyIcon(shfileinfo.hIcon); shfileinfo.hIcon = IntPtr.Zero; return(imageSource); }
public static ImageSource GetDirectoryIcon(string path, int iconIndex, IconSize size = IconSize.Small) { ImageSource imageSource = null; if (IconsDictionary.TryGetValue(iconIndex, out imageSource)) { return imageSource; } Int32Rect sizeRect; WinAPI.SHGFI flags; if (IconSize.Small == size) { flags = commonFlags | WinAPI.SHGFI.SHGFI_SMALLICON; sizeRect = new Int32Rect(0, 0, 16, 16); } else { flags = commonFlags | WinAPI.SHGFI.SHGFI_LARGEICON; sizeRect = new Int32Rect(0, 0, 32, 32); } WinAPI.SHFILEINFO shfileinfo = new WinAPI.SHFILEINFO(); WinAPI.SHGetFileInfo(path, 256, out shfileinfo, (uint) Marshal.SizeOf(shfileinfo), flags); if (shfileinfo.hIcon == IntPtr.Zero) { return GetIcon(path); } imageSource = Imaging.CreateBitmapSourceFromHIcon(shfileinfo.hIcon, sizeRect, BitmapSizeOptions.FromEmptyOptions()); IconsDictionary.Add(iconIndex, imageSource); WinAPI.DestroyIcon(shfileinfo.hIcon); shfileinfo.hIcon = IntPtr.Zero; return imageSource; }
public NativeFileInfo(string path) { Path = path; shInfo = new WinAPI.SHFILEINFO(); WinAPI.SHGetFileInfo(path, WinAPI.FILE_ATTRIBUTE_NORMAL, out shInfo, (uint)Marshal.SizeOf(shInfo), vFlags); }