Results 1 to 2 of 2

Thread: hDC to stdPicture

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Ha noi
    Posts
    2

    Angry hDC to stdPicture

    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.
    quadratic

  2. #2
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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:
    VB Code:
    1. Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    2. Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    3. Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    4.  
    5. 'to get the handle to the bitmap in your DC (assuming hDC is the variable that represents the DC)
    6. Dim hBitmap As Long
    7. Dim hOldBitmap As Long
    8. Dim picNew As StdPicture
    9.  
    10. 'create a blank 1x1 bitmap
    11. hBitmap = CreateCompatibleBitmap(hDC, 1, 1)
    12.  
    13. 'put this blank bitmap into the DC and save the one that was in it
    14. hOldBitmap = SelectObject(hDC, hBitmap)
    15.  
    16. 'create a new stdpicture object from that handle: the function name is the same, but you'll need
    17. 'to modify it like I mention in the other thread
    18. Set picNew = IconToPicture(hOldBitmap)
    19.  
    20. 'restore the old bitmap and delete the created one
    21. 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.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width