PDA

Click to See Complete Forum and Search --> : hDC to stdPicture


quadratic
Feb 22nd, 2002, 01:58 AM
How can I do to create stdPicture object from a specified device context. For example I have a picture box, then I draw something on it. please help me to create a stdole.stdPicture object that I can use it to load on other picture box. Thanks a lot.

Kaverin
Feb 24th, 2002, 05:11 PM
You could just directly BitBlt() the image from one picturebox to the other, but if you want the new stdpicture object you can do this. It will use a function that is in some stuff I posted earlier in the following thread http://www.vbforums.com/showthread....&threadid=87710
It will need to be modified to handle a handle to a bitmap, but I mention how to do that in the message.

To get the bitmap handle from the DC you'll need to do this:

Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

'to get the handle to the bitmap in your DC (assuming hDC is the variable that represents the DC)
Dim hBitmap As Long
Dim hOldBitmap As Long
Dim picNew As StdPicture

'create a blank 1x1 bitmap
hBitmap = CreateCompatibleBitmap(hDC, 1, 1)

'put this blank bitmap into the DC and save the one that was in it
hOldBitmap = SelectObject(hDC, hBitmap)

'create a new stdpicture object from that handle: the function name is the same, but you'll need
'to modify it like I mention in the other thread
Set picNew = IconToPicture(hOldBitmap)

'restore the old bitmap and delete the created one
DeleteObject SelectObject(hDC, hOldBitmap)


Now you should have a reference to a new stdpicture object which has the info from the bitmap that was inside the DC. This is the general case, but if you were using pictureboxes you could use picturebox.hDC and picturebox.Picture.Handle for the DC and bitmap handles, respectively. When you're done with that new pictuer object though, don't forget to set it to Nothing.