4

My NET 2.0 Winforms app works beautifully on Vista and Windows 7 but a call to Bitmap.GetHbitmap() returns null on Windows XP (even with SP3). The underlying Bitmap is a PNG and is loaded from resources. It is loaded correctly so it is down to GetHbitmap(). I have tried calling both overloads with the same result.

stivlo
  • 83,644
  • 31
  • 142
  • 199
wpfwannabe
  • 14,587
  • 16
  • 78
  • 129

1 Answers1

0

Watch out for memory leaks while debugging and working with .GetHBitmap

When you're using this function you need to delete the object manually!!

MSDN example: http://msdn.microsoft.com/en-us/library/1dz311e4.aspx

<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
    Private Shared Function DeleteObject (ByVal hObject As IntPtr) As Boolean
    End Function



    Private Sub DemonstrateGetHbitmap()
        Dim bm As New Bitmap("Picture.jpg")
        Dim hBitmap As IntPtr
        hBitmap = bm.GetHbitmap()

        ' Do something with hBitmap.
        DeleteObject(hBitmap)
    End Sub

and similar question: Image loading memory leak with C#

Regards

Community
  • 1
  • 1
Nasenbaer
  • 4,810
  • 11
  • 53
  • 86