Groups | Blog | Home
all groups > dotnet drawing api > february 2006 >

dotnet drawing api : GetHBitmap and FromHBitmap


tedunni NO[at]SPAM gmail.com
2/28/2006 12:31:03 PM
I have the scenario below where I have a New Bitmap object and another
bitmap that points to this object. Later on, I need to supply a GDI
handle to a C routine. I get the GDI handle using GHBitmap, and then
to get the Image back I call FromHBitmap. However, when I call
FromHBitmap, I then break my reference to the Original BMP. Is there a
way for me to set the Image that my pointer is pointing to equal to the
Image returned from GetHBitmap?

==========================================
Dim obj As New Bitmap("c:\file.bmp")
Dim ptrBitmap As Bitmap
'ptrBitmap points to obj
ptrBitmap = obj

Dim hbmp As IntPtr
hBmp = ptrBitmap.GetHbitmap
'The C routine needs a GDI handle, it changes the image
C_Dll.Some_C_Routine_That_Takes_A_GDI_Handle(hbmp)

'I need to get the Modified Image that the C routine changed
ptrBitmap = Bitmap.FromHbitmap(hbmp)

'Now ptrBitmap no longer points to obj which is a problem

'Is there a way for me to set the the image that ptrBitmap is pointing
equal to the Image returned from Bitmap.FromHBitmap(hbmp)?
'Or
'Is there as way for me to have hBmp not create a new object and have
the C routine modify the Image that is in obj?
==========================================

Thanks, and any help would be appreciated.
Kevin Westhead
3/28/2006 11:28:05 PM
I'm not sure if this is possible. System.Drawing.Bitmap is a .NET wrapper
for a GDI+ bitmap rather than a GDI bitmap. This means that when you call
GetHbitmap, you are actually creating a GDI bitmap from a GDI+ bitmap rather
than simply returning a handle to the GDI+ bitmap. This also means that
you're responsible for cleaning up the GDI bitmap allocated as a result of
calling GetHbitmap. You might want to check the GDI objects count in Task
Manager when your code is running just to confirm that the calls to
GetHbitmap aren't causing you to leak GDI bitmaps.

--
Kevin Westhead

[quoted text, click to view]

AddThis Social Bookmark Button