Draw image to PictureBox given hDC

How do I load an image into a PictureBox only knowing that control's hDC?
Who is Participating?
bobbit31Connect With a Mentor Commented:
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source

Private Sub Command1_Click()
Picture1.ScaleMode = 3
Picture1.Picture = LoadPicture("C:\my documents\badbaby.bmp")

'' now write to picture2 hdc
BitBlt Picture2.hDC, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, _
       Picture1.hDC, 0, 0, SRCCOPY

End Sub

though you probably won't be able to get Picture2.ScaleWidth, Picture2.ScaleHeight since all you have is the hdc... but all that is is the destination width/height which you can set to whatever you want.
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.SetData p
SendMessage RichTextBox1.hwnd, WM_PASTE, 0, 0

RichTextBox1.SelPrint (Picture1.hDC)

End Sub
You may try the BitBlt API function to transfer an image from one hDC to another.  Example can be found here:
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Richie_SimonettiIT OperationsCommented:
From where you are getting the image in first place?
dplambertAuthor Commented:
The source image will come from another PictureBox control.
Richie_SimonettiIT OperationsCommented:
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.
dplambertAuthor Commented:
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
Richie_SimonettiIT OperationsCommented:
OK. So bitblt form Bobbit could do the job.
dplambertAuthor Commented:
My research on the web also show this to be the "best" way for what I want to do too... Thanks...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.