8

I have am using a WPF application which uses BitmapSource but I need to do some manipulation but I need to do some manipulation of System.Drawing.Bitmaps.

The memory use of the application increases while it runs.

I have narrowed down the memory leak to this code:

private BitmapSource BitmaptoBitmapsource(System.Drawing.Bitmap bitmap)
{
            BitmapSource bms;
            IntPtr hBitmap = bitmap.GetHbitmap();
            BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
            bms = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions);
            bms.Freeze();
            return bms;
}

I assume it is the unmanaged memory not being disposed of properly, but I cannot seem to find anyway of doing it manually. Thanks in advance for any help!

Alex

Chris E
  • 973
  • 13
  • 26
aforward
  • 133
  • 1
  • 11
  • possible duplicate of [WPF CreateBitmapSourceFromHBitmap memory leak](http://stackoverflow.com/questions/1546091/wpf-createbitmapsourcefromhbitmap-memory-leak) – Pieniadz May 28 '14 at 08:59

4 Answers4

10

You need to call DeleteObject(...) on your hBitmap. See: http://msdn.microsoft.com/en-us/library/1dz311e4.aspx

private BitmapSource BitmaptoBitmapsource(System.Drawing.Bitmap bitmap)
{
    BitmapSource bms;
    IntPtr hBitmap = bitmap.GetHbitmap();
    BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
    bms = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, 
        IntPtr.Zero, Int32Rect.Empty, sizeOptions);
    bms.Freeze();

    // NEW:
    DeleteObject(hBitmap);

    return bms;
}
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • 3
    I was about to write exactly the same answer ;) Here's the declaration of the `DeleteObject` method: `[DllImport("gdi32.dll")] static extern bool DeleteObject(IntPtr hObject);` – ken2k Dec 29 '11 at 16:11
  • @ken2k: and I was about to add exactly the same declaration. Thanks! – MusiGenesis Dec 29 '11 at 16:13
4

You need to call DeleteObject(hBitmap) on the hBitmap:

private BitmapSource BitmaptoBitmapsource(System.Drawing.Bitmap bitmap) {
        BitmapSource bms;
        IntPtr hBitmap = bitmap.GetHbitmap();
        BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
        try {
            bms = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions);
            bms.Freeze();
        } finally {
            DeleteObject(hBitmap);
        }
        return bms;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The MSDN says You are responsible for calling the GDI DeleteObject method to free the memory used by the GDI bitmap object.. The following question deals with the same problem and there is already answer WPF CreateBitmapSourceFromHBitmap memory leak

Community
  • 1
  • 1
Toni Parviainen
  • 2,217
  • 1
  • 16
  • 15
0

Are you releasing bitmap handle?

According to MSDN (http://msdn.microsoft.com/en-us/library/1dz311e4.aspx)

You are responsible for calling the GDI DeleteObject method to free the memory used by the GDI bitmap object. For more information about GDI bitmaps, see Bitmaps in the Windows GDI documentation.

Novakov
  • 3,055
  • 1
  • 16
  • 32