DeleteObject() private method

private DeleteObject ( IntPtr hObject ) : bool
hObject System.IntPtr
return bool
        public static Bitmap CopyHBitmapToBitmap(IntPtr nativeHBitmap)
        {
            // Get width, height and the address of the pixel data for the native HBitmap
            BITMAP bitmapStruct = new BITMAP();

            ShellNativeMethods.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
            ShellNativeMethods.DeleteObject(nativeHBitmap);

            // Return the managed bitmap, clone of the native HBitmap, with correct transparency
            return(managedBitmapReal);
        }
Beispiel #2
0
        private Bitmap GetBitmap(System.Windows.Size size)
        {
            IntPtr hBitmap = GetHBitmap(size);

            // return a System.Drawing.Bitmap from the hBitmap
            Bitmap returnValue = Bitmap.FromHbitmap(hBitmap);

            // delete HBitmap to avoid memory leaks
            ShellNativeMethods.DeleteObject(hBitmap);

            return(returnValue);
        }
Beispiel #3
0
        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.
            BitmapSource returnValue = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            // delete HBitmap to avoid memory leaks
            ShellNativeMethods.DeleteObject(hBitmap);

            return(returnValue);
        }