?
Solved

Draw image to PictureBox given hDC

Posted on 2002-05-23
10
Medium Priority
?
1,074 Views
Last Modified: 2008-03-10
How do I load an image into a PictureBox only knowing that control's hDC?
0
Comment
Question by:dplambert
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 7029859
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
0
 
LVL 3

Expert Comment

by:nichia
ID: 7029902
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7030082
From where you are getting the image in first place?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Author Comment

by:dplambert
ID: 7030270
The source image will come from another PictureBox control.
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 300 total points
ID: 7030339
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7030415
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.
0
 
LVL 3

Author Comment

by:dplambert
ID: 7030487
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...
0
 
LVL 6

Expert Comment

by:pierrecampe
ID: 7030506
On Error Resume Next
For Each Control In Controls
    If Control.hDC = ThehDC Then
       Control.Picture = Picture1.Picture
    End If
Next
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7030525
OK. So bitblt form Bobbit could do the job.
0
 
LVL 3

Author Comment

by:dplambert
ID: 7030589
My research on the web also show this to be the "best" way for what I want to do too... Thanks...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question