VB Code:
  1. 'in a module
  2. Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  3. Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  4. Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
  5.  
  6. 'when you need a handle to the bitmap in memoryDC
  7. Dim hBitmap As Long, hDummyBitmap As Long
  8. 'make a 1x1 pixel bitmap compatible with the memory DC
  9. hDummyBitmap = CreateCompatibleBitmap(memoryDC, 1, 1)
  10. 'put that bitmap into the DC, which causes the handle to the old one to be returned
  11. hBitmap = SelectObject(memoryDC, hDummyBitmap)
  12. 'put that bitmap back into the DC
  13. SelectObject memoryDC, hBitmap
  14. 'delete the dummy bitmap
  15. DeleteObject hDummyBitmap
  16.  
  17. 'at this point, your DC is unchanged, but you now have the handle to the bitmap that was in it
  18. 'hBitmap is the handle