Beispiel #1
0
        public static ImageSource GetThumbInt(string path, int width, int height, ThumbOptions opt)
        {
            IShellItem shellitem;
            var shellGuid = new Guid(ShellItemGuid);
            int ret = SHCreateItemFromParsingName(path, IntPtr.Zero, ref shellGuid, out shellitem);
            if (ret != 0)
                throw Marshal.GetExceptionForHR(ret);
            var nativeSize = new NativeSize
            {
                Width = width,
                Height = height
            };

            IntPtr hBitmap;
            var shellItemImageFactory = (IShellItemImageFactory) shellitem;
            if (shellItemImageFactory == null) return null;
            HResult hr = shellItemImageFactory.GetImage(nativeSize, opt, out hBitmap);

            Marshal.ReleaseComObject(shellitem);

            if (hr != HResult.Ok)
            {
                throw Marshal.GetExceptionForHR((int)hr);
            }

            ImageSource imgsrc;
            try
            {
                imgsrc = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            }
            finally
            {
                DeleteObject(hBitmap);
            }
            return imgsrc;
        }
        private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
        {
            IShellItem nativeShellItem;
            Guid shellItem2Guid = new Guid(IShellItem2Guid);
            int retCode = SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref shellItem2Guid, out nativeShellItem);

            if (retCode != 0)
                throw Marshal.GetExceptionForHR(retCode);

            NativeSize nativeSize = new NativeSize();
            nativeSize.Width = width;
            nativeSize.Height = height;

            IntPtr hBitmap;
            HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap);

            Marshal.ReleaseComObject(nativeShellItem);

            if (hr == HResult.Ok) return hBitmap;

            throw Marshal.GetExceptionForHR((int)hr);
        }