private ImageSource GetDisplayIcon(IconSize size) { ImageSource icon = null; IShellItemImageFactory imageFactory = GetImageFactory(AbsolutePidl); if (imageFactory == null) { icon = IconImageConverter.GetDefaultIcon(); } else { try { int iconPoints = IconHelper.GetSize(size); SIZE imageSize = new SIZE { cx = (int)(iconPoints * DpiHelper.DpiScale), cy = (int)(iconPoints * DpiHelper.DpiScale) }; IntPtr hBitmap = IntPtr.Zero; SIIGBF flags = 0; if (size == IconSize.Small) { // for 16pt icons, thumbnails are too small flags = SIIGBF.ICONONLY; } if (imageFactory?.GetImage(imageSize, flags, out hBitmap) == NativeMethods.S_OK) { if (hBitmap != IntPtr.Zero) { icon = IconImageConverter.GetImageFromHBitmap(hBitmap); } } } catch (Exception e) { ShellLogger.Error($"ShellItem: Unable to get icon from ShellItemImageFactory: {e.Message}"); } finally { Marshal.FinalReleaseComObject(imageFactory); } } if (icon == null) { // Fall back to SHGetFileInfo icon = IconImageConverter.GetImageFromAssociatedIcon(AbsolutePidl, size); } if (icon == null) { icon = IconImageConverter.GetDefaultIcon(); } return(icon); }
public ShellItemImageFactory(IShellItemImageFactory shllItemImageFactoryInterface) { Contract.Requires <ArgumentNullException>(shllItemImageFactoryInterface != null); this.shllItemImageFactoryInterface = shllItemImageFactoryInterface; this.SizeOption = ShellItemImageSizeOptions.ResizeToFit; this.RetrievalOption = ShellItemImageRetrievalOptions.Default; this.FormatOption = ShellItemImageFormatOptions.Default; }
public static extern int SHCreateItemFromParsingNameIShellItemImageFactory( [In][MarshalAs(UnmanagedType.LPWStr)] string pszPath, [In] IntPtr pbc, [In][MarshalAs(UnmanagedType.LPStruct)] Guid riid, [Out][MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] out IShellItemImageFactory ppv);
public Bitmap LoadThumbnailFromImageFactory(string filename, Size requestedSize, bool onlyThumbnail) { if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename)) { return(null); } Bitmap bm1 = null; IntPtr hbitmap = IntPtr.Zero; SIZE sz = new SIZE(requestedSize.Width, requestedSize.Height); IShellFolder desktop = null; IntPtr pidlMain = IntPtr.Zero; IShellItem shellItem = null; IShellItemImageFactory ppsiShellItemImageFactory = null; try { Shell32.SHGetDesktopFolder(ref desktop); int cParsed = 0; int pdwAttrib = 0; desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filename, out cParsed, out pidlMain, out pdwAttrib); //Shell32.SHCreateItemFromIDList(pidlMain, uuidIShellItem, out shellItem); Shell32.SHCreateShellItem(IntPtr.Zero, null, pidlMain, out shellItem); ppsiShellItemImageFactory = (IShellItemImageFactory)shellItem; if (onlyThumbnail) { ppsiShellItemImageFactory.GetImage(sz, SIIGBF.SIIGBF_BIGGERSIZEOK | SIIGBF.SIIGBF_THUMBNAILONLY, out hbitmap); } else { ppsiShellItemImageFactory.GetImage(sz, SIIGBF.SIIGBF_BIGGERSIZEOK, out hbitmap); } bm1 = GetBitmapFromHbitmap(hbitmap); } catch (InvalidCastException) { Debug.Write(" Did not support IShellItemImageFactory\r\n"); } catch (Exception e) { Debug.Write(" Exception extracting image from IShellItemImageFactory: " + e.Message + "\r\n"); } finally { if (ppsiShellItemImageFactory != null) { Marshal.ReleaseComObject(ppsiShellItemImageFactory); ppsiShellItemImageFactory = null; } if (shellItem != null) { Marshal.ReleaseComObject(shellItem); shellItem = null; } if (pidlMain != IntPtr.Zero) { Allocator.Free(pidlMain); pidlMain = IntPtr.Zero; } if (desktop != null) { Marshal.ReleaseComObject(desktop); desktop = null; } if (_allocator != null) { Marshal.ReleaseComObject(_allocator); _allocator = null; } } return(bm1); }
private static extern int SHCreateItemFromParsingName(string path, IntPtr pbc, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IShellItemImageFactory factory);
public static extern void SHCreateItemFromIDList( [In] IntPtr pidl, [In][MarshalAs(UnmanagedType.LPStruct)] Guid riid, [Out][MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] out IShellItemImageFactory ppv);