public static ImageSource GetImageSourceForPath(string path) { IntPtr hImgSmall; //the handle to the system image list IntPtr hImgLarge; //the handle to the system image list string fName; // 'the file name to get icon from SHFILEINFO shinfo = new SHFILEINFO(); //Use this to get the small Icon // hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_ICON |Win32.SHGFI_SMALLICON); //Use this to get the large Icon hImgLarge = Win32.SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON); //The icon is returned in the hIcon member of the shinfo struct var myIcon = Icon.FromHandle(shinfo.hIcon); ImageSource img; using (Icon i = Icon.FromHandle(shinfo.hIcon)) { img = Imaging.CreateBitmapSourceFromHIcon( i.Handle, new Int32Rect(0, 0, i.Width, i.Height), BitmapSizeOptions.FromEmptyOptions()); } return img; }
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);