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 static Bitmap CopyHBitmapToBitmap(IntPtr nativeHBitmap) { // Get width, height and the address of the pixel data for the native HBitmap BITMAP bitmapStruct = new BITMAP(); Gdi32.GetObjectBitmap(nativeHBitmap, Marshal.SizeOf(bitmapStruct), ref bitmapStruct); // Create a managed bitmap that has its pixel data pointing to the pixel data of the native HBitmap // No memory is allocated for its pixel data Bitmap managedBitmapPointer = new Bitmap( bitmapStruct.bmWidth, bitmapStruct.bmHeight, bitmapStruct.bmWidth * 4, PixelFormat.Format32bppArgb, bitmapStruct.bmBits); // Create a managed bitmap and allocate memory for pixel data Bitmap managedBitmapReal = new Bitmap(bitmapStruct.bmWidth, bitmapStruct.bmHeight, PixelFormat.Format32bppArgb); // Copy the pixels of the native HBitmap into the canvas of the managed bitmap Graphics graphics = Graphics.FromImage(managedBitmapReal); graphics.DrawImage(managedBitmapPointer, 0, 0); graphics.Dispose(); // Delete the native HBitmap object and free memory Gdi32.DeleteObject(nativeHBitmap); // Return the managed bitmap, clone of the native HBitmap, with correct transparency return(managedBitmapReal); }
private Bitmap GetBitmap(System.Windows.Size size) { IntPtr hBitmap = GetHBitmap(size); // return a System.Drawing.Bitmap from the hBitmap Bitmap returnValue = null; if (hBitmap != IntPtr.Zero) { returnValue = GetBitmapFromHBitmap(hBitmap); } // delete HBitmap to avoid memory leaks Gdi32.DeleteObject(hBitmap); return(returnValue); }
private BitmapSource GetBitmapSource(System.Windows.Size size) { IntPtr hBitmap = GetHBitmap(size); // return a System.Media.Imaging.BitmapSource // Use interop to create a BitmapSource from hBitmap. if (hBitmap != IntPtr.Zero) { BitmapSource returnValue = Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); // delete HBitmap to avoid memory leaks Gdi32.DeleteObject(hBitmap); return(returnValue); } return(null); }
private BitmapSource GetBitmapSource(System.Windows.Size size, Boolean isCopyItem = false) { //FIXME: fix the cache retrieval options //RetrievalOption = ShellThumbnailRetrievalOption.Default; IntPtr hBitmap = GetHBitmap(size, isCopyItem); // return a System.Media.Imaging.BitmapSource // Use interop to create a BitmapSource from hBitmap. if (hBitmap != IntPtr.Zero) { BitmapSource returnValue = Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()).Clone(); // delete HBitmap to avoid memory leaks Gdi32.DeleteObject(hBitmap); return(returnValue); } return(null); }