Solved

Draw image to PictureBox given hDC

Posted on 2002-05-23
10
1,005 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 75 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help formating a date variable in my VB.Net application 2 55
passing parameter in sql procedure 9 63
Sending a email via excel using vba 6 85
Problem to With line 4 57
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

825 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