示例#1
0
        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);
        }