public HResult ExtractAndDrawThumbnail(IntPtr hdc, uint iconSize, out WTS_CACHEFLAGS flags, User32.RECT iconBounds, out bool retrieved, bool isHidden, bool isRefresh = false) { HResult res = HResult.S_OK; ISharedBitmap bmp = null; flags = WTS_CACHEFLAGS.WTS_DEFAULT; WTS_THUMBNAILID thumbId = new WTS_THUMBNAILID(); try { retrieved = false; res = ThumbnailCache.GetThumbnail(this._Item.ComInterface, iconSize, isRefresh ? (WTS_FLAGS.WTS_FORCEEXTRACTION | WTS_FLAGS.WTS_SCALETOREQUESTEDSIZE) : (WTS_FLAGS.WTS_INCACHEONLY | WTS_FLAGS.WTS_SCALETOREQUESTEDSIZE), out bmp, flags, thumbId); IntPtr hBitmap = IntPtr.Zero; if (bmp != null) { bmp.GetSharedBitmap(out hBitmap); retrieved = true; int width; int height; Gdi32.ConvertPixelByPixel(hBitmap, out width, out height); Gdi32.NativeDraw(hdc, hBitmap, iconBounds.Left + (iconBounds.Right - iconBounds.Left - width) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - height) / 2, width, height, isHidden); Gdi32.DeleteObject(hBitmap); } } finally { if (bmp != null) { Marshal.ReleaseComObject(bmp); } } return(res); }
public void PreloadThumbnail(string filePath) { IShellItem shellItem = null; ISharedBitmap bmp = null; WTS_CACHEFLAGS cFlags; WTS_THUMBNAILID bmpId; try { SHCreateItemFromParsingName(filePath, IntPtr.Zero, iIdIShellItem, out shellItem); TBCache.GetThumbnail(shellItem, 128, WTS_FLAGS.WTS_EXTRACTINPROC, out bmp, out cFlags, out bmpId); } catch (Exception) { //Do nothing } if (bmp != null) { Marshal.ReleaseComObject(bmp); } if (shellItem != null) { Marshal.ReleaseComObject(shellItem); } bmp = null; shellItem = null; }
public HResult ExtractAndDrawThumbnail(IntPtr hdc, uint iconSize, out WTS_CACHEFLAGS flags, User32.RECT iconBounds, out bool retrieved, bool isHidden, bool isRefresh = false) { IThumbnailCache thumbCache = null; if (this.ComInterface != null) { var IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046"); var CLSID_LocalThumbnailCache = new Guid("50EF4544-AC9F-4A8E-B21B-8A26180DB13F"); IntPtr cachePointer; Ole32.CoCreateInstance(ref CLSID_LocalThumbnailCache, IntPtr.Zero, Ole32.CLSCTX.INPROC, ref IID_IUnknown, out cachePointer); thumbCache = (IThumbnailCache)Marshal.GetObjectForIUnknown(cachePointer); } var res = HResult.S_OK; ISharedBitmap bmp = null; flags = WTS_CACHEFLAGS.WTS_DEFAULT; var thumbId = default(WTS_THUMBNAILID); try { retrieved = false; res = thumbCache.GetThumbnail(this._Item.ComInterface, iconSize, isRefresh ? (WTS_FLAGS.WTS_FORCEEXTRACTION | WTS_FLAGS.WTS_SCALETOREQUESTEDSIZE) : (WTS_FLAGS.WTS_INCACHEONLY | WTS_FLAGS.WTS_SCALETOREQUESTEDSIZE), out bmp, flags, thumbId); var hBitmap = IntPtr.Zero; if (bmp != null) { bmp.GetSharedBitmap(out hBitmap); retrieved = true; int width; int height; Gdi32.ConvertPixelByPixel(hBitmap, out width, out height); Gdi32.NativeDraw(hdc, hBitmap, iconBounds.Left + (iconBounds.Right - iconBounds.Left - width) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - height) / 2, width, height, isHidden); Gdi32.DeleteObject(hBitmap); } } finally { if (bmp != null) { Marshal.ReleaseComObject(bmp); } } return(res); }
public Boolean RefreshThumbnail(uint iconSize, out WTS_CACHEFLAGS flags) { ISharedBitmap bmp = null; WTS_CACHEFLAGS cacheFlags = WTS_CACHEFLAGS.WTS_DEFAULT; WTS_THUMBNAILID thumbId = new WTS_THUMBNAILID(); Boolean result = false; try { if (ThumbnailCache.GetThumbnail(this.shellItemNative, iconSize, WTS_FLAGS.WTS_FORCEEXTRACTION | WTS_FLAGS.WTS_SCALETOREQUESTEDSIZE, out bmp, cacheFlags, thumbId) != HResult.WTS_E_FAILEDEXTRACTION) { result = true; } } finally { if (bmp != null) { Marshal.ReleaseComObject(bmp); } } flags = cacheFlags; return(result); }
private static ImageData LoadThumbnail(string path, DateTime dtLastWriteTime, out Size sizeRaw, out Size sizeActual, out string toolTipText, out bool fCached) { sizeRaw = sizeActual = Size.Empty; toolTipText = null; fCached = false; IntPtr zero = IntPtr.Zero; IShellItem ppsi = null; ISharedBitmap ppvThumb = null; LocalThumbnailCache o = null; try { zero = PInvoke.ILCreateFromPath(path); if ((zero != IntPtr.Zero) && (PInvoke.SHCreateShellItem(IntPtr.Zero, null, zero, out ppsi) == 0)) { o = new LocalThumbnailCache(); IThumbnailCache cache2 = (IThumbnailCache)o; uint flags = 0; uint pOutFlags = 0; WTS_THUMBNAILID pThumbnailID = new WTS_THUMBNAILID(); uint cxyRequestedThumbSize = (uint)Math.Min(0x400, Math.Min(QTUtility.PreviewMaxWidth, QTUtility.PreviewMaxHeight)); if (cache2.GetThumbnail(ppsi, cxyRequestedThumbSize, flags, out ppvThumb, ref pOutFlags, ref pThumbnailID) == 0) { IntPtr ptr2; if ((pOutFlags & 2) == 2) { fCached = true; } if (ppvThumb.Detach(out ptr2) == 0) { Bitmap bmp = Image.FromHbitmap(ptr2); Size size = bmp.Size; sizeRaw = sizeActual = size; ImageData data = new ImageData(bmp, null, path, dtLastWriteTime, size, size); data.Thumbnail = true; try { toolTipText = data.TooltipText = ShellMethods.GetShellInfoTipText(zero, false); } catch { } return(data); } } } } catch (Exception exception) { QTUtility2.MakeErrorLog(exception, null); } finally { if (zero != IntPtr.Zero) { PInvoke.CoTaskMemFree(zero); } if (ppsi != null) { Marshal.ReleaseComObject(ppsi); } if (ppvThumb != null) { Marshal.ReleaseComObject(ppvThumb); } if (o != null) { Marshal.ReleaseComObject(o); } } return(null); }
public System.Windows.Media.ImageSource GetThumbnail(string imagePath, ThumbnailQuality quality) { IShellItem shItem; Guid iIdIShellItem = new Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe"); var hr = SHCreateItemFromParsingName(imagePath, IntPtr.Zero, iIdIShellItem, out shItem); if (hr == 0) { Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046"); Guid CLSID_LocalThumbnailCache = new Guid("50EF4544-AC9F-4A8E-B21B-8A26180DB13F"); var type = Type.GetTypeFromCLSID(CLSID_LocalThumbnailCache); var tbCache = (IThumbnailCache)Activator.CreateInstance(type); ISharedBitmap bmp = null; WTS_CACHEFLAGS cFlags; WTS_THUMBNAILID bmpId; uint qualitySetting; switch (quality) { case ThumbnailQuality.Normal: qualitySetting = 96; break; case ThumbnailQuality.High: case ThumbnailQuality.Full: qualitySetting = 128; break; default: qualitySetting = 96; break; } hr = (int)tbCache.GetThumbnail(shItem, qualitySetting, WTS_FLAGS.WTS_EXTRACT, out bmp, out cFlags, out bmpId); if (hr == 0) { var bmpPtr = IntPtr.Zero; if (bmp.Detach(out bmpPtr) == 0) { try { var iSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bmpPtr, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(bmpPtr); iSrc.Freeze(); return(iSrc); } finally { bmpPtr = IntPtr.Zero; Marshal.ReleaseComObject(bmp); Marshal.ReleaseComObject(shItem); bmp = null; shItem = null; } } else { Marshal.ReleaseComObject(bmp); Marshal.ReleaseComObject(shItem); return(null); } } else { Marshal.ReleaseComObject(shItem); return(null); } } return(null); }