How can I remove DeleteObject () on hBitmap except using P / Invoke?

I am dealing with code that looks something like this (from here )

using (var bmp = new System.Drawing.Bitmap(1000, 1000)) 
{
    IntPtr hBitmap = bmp.GetHbitmap(); 
    var source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
        hBitmap, IntPtr.Zero, Int32Rect.Empty,
        System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}

and MSDN says that whenever I call Bitmap.GetHbitmap(), I have to call DeleteObject()in hBitmapto free up unmanaged resources.

All the answers I saw and MSDN say that I should P / call the function DeleteObject(). It looks a little crazy because I got it hBitmapwithout P / Invoke, and now I suddenly need to use P / Invoke to get rid of it correctly.

Is there no other way than P / Invoke to DeleteObject()call mine hBitmap?

+4
1

, DeleteObject #, p/invoke.

, , GetHbitmap . . BitmapSource. BitmapSource.Create.

+4

All Articles