private IntPtr GetHBitmap(string fileName, int width, int height, Win32Helper.ThumbnailOptions options) { Win32Helper.IShellItem nativeShellItem; Guid shellItem2Guid = new Guid(Win32Helper.IShellItem2Guid); int retCode = Win32Helper.SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref shellItem2Guid, out nativeShellItem); if (retCode != 0) { throw Marshal.GetExceptionForHR(retCode); } Win32Helper.NativeSize nativeSize = new Win32Helper.NativeSize(); nativeSize.Width = width; nativeSize.Height = height; IntPtr hBitmap; Win32Helper.HResult hr = ((Win32Helper.IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap); Marshal.ReleaseComObject(nativeShellItem); if (hr == Win32Helper.HResult.Ok) { return(hBitmap); } throw Marshal.GetExceptionForHR((int)hr); }
private Bitmap GetBitmapThumbnail(string fileName, int width = 80, int height = 80, Win32Helper.ThumbnailOptions options = Win32Helper.ThumbnailOptions.None) { IntPtr hBitmap = GetHBitmap(System.IO.Path.GetFullPath(fileName), width, height, options); try { Bitmap bmp = Bitmap.FromHbitmap(hBitmap); if (Bitmap.GetPixelFormatSize(bmp.PixelFormat) < 32) { return(bmp); } return(CreateAlphaBitmap(bmp, System.Drawing.Imaging.PixelFormat.Format32bppArgb)); } finally { // delete HBitmap to avoid memory leaks Win32Helper.DeleteObject(hBitmap); } }
public string GetJPGThumbnail(string filename, int width = 80, int height = 80, Win32Helper.ThumbnailOptions options = Win32Helper.ThumbnailOptions.None) { if (!File.Exists(filename)) { return(string.Empty); } Bitmap bit = GetBitmapThumbnail(filename, width, height, options); if (bit == null) { return(string.Empty); } return(GetThumbnailFilePath(bit)); }