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.
Asked
Active
Viewed 767 times
4

stivlo
- 83,644
- 31
- 142
- 199

wpfwannabe
- 14,587
- 16
- 78
- 129
-
1Just to clarify, you're not getting `null` but `IntPtr.Zero` instead, right? – Chris Haas Apr 28 '11 at 12:53
-
Try updating the relevant DLL: http://www.microsoft.com/downloads/en/details.aspx?familyid=6A63AB9C-DF12-4D41-933C-BE590FEAA05A&displaylang=en – Shadow The GPT Wizard Apr 28 '11 at 12:53
-
Oops. Sorry. That is correct. Not `null` but `IntPtr.Zero`. – wpfwannabe Apr 28 '11 at 13:00
-
Could you post some code? I just tried on XP SP3 and I get a valid HBITMAP... – Thomas Levesque Apr 28 '11 at 13:03
-
Hm... I must have forgotten my medication. I can't reproduce this anymore no matter how hard I try. I do have another issue though which deserves a separate question. Thanks for everybody's help. – wpfwannabe Apr 28 '11 at 14:28
-
2can you close this question if it's no longer relevant? – justin.m.chase Aug 16 '11 at 19:48
-
Sure, but none of the reasons offered for closing reflect my closing intention. – wpfwannabe Aug 17 '11 at 10:14
-
@wpfwannabe: ... and yet, it remains in the "unanswered questions" queue. – Marc L. Oct 27 '11 at 02:24
-
@MarcL. I just voted. It needs more votes to be closed. I can't seem to just close it myself. – wpfwannabe Oct 27 '11 at 21:23
1 Answers
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