Link to home
Start Free TrialLog in
Avatar of dplambert
dplambert

asked on

Draw image to PictureBox given hDC

How do I load an image into a PictureBox only knowing that control's hDC?
Avatar of bobbit31
bobbit31
Flag of United States of America image

if you don't mind messing w/ the clipboard you could do something like this:

put a richtextbox on your form (invisible if you want) and do the following:

Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long
Private Const WM_PASTE = &H302


Private Sub Command1_Click()

Dim p As StdPicture
Set p = LoadPicture("C:\my documents\badbaby.bmp")

Clipboard.Clear
Clipboard.SetData p
SendMessage RichTextBox1.hwnd, WM_PASTE, 0, 0

RichTextBox1.SelPrint (Picture1.hDC)


End Sub
Avatar of nichia
nichia

You may try the BitBlt API function to transfer an image from one hDC to another.  Example can be found here:
http://216.26.161.91/vbapi/ref/b/bitblt.html
Avatar of Richie_Simonetti
From where you are getting the image in first place?
Avatar of dplambert

ASKER

The source image will come from another PictureBox control.
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Stange. I don't understand why do you need to use hdc to put an image from one picture box to another since
Picture2.Picture = Picture1.Picture could do the job.
Yeah, good point. I might just do that. I'm just trying to be consistent with the project style. All PictureBoxes are updated by passing a hDC to a DLL that handles the graphics. In a module, a call is made to the DLL and this method accepts a hDC. Under a certain case, I was going to use the hDC myself to put my own graphic in here, but I could do it that way too...
On Error Resume Next
For Each Control In Controls
    If Control.hDC = ThehDC Then
       Control.Picture = Picture1.Picture
    End If
Next
OK. So bitblt form Bobbit could do the job.
My research on the web also show this to be the "best" way for what I want to do too... Thanks...