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); }
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; }