Solved

Draw image to PictureBox given hDC

Posted on 2002-05-23
10
982 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
Comment Utility
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
Comment Utility
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
Comment Utility
From where you are getting the image in first place?
0
 
LVL 3

Author Comment

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

Accepted Solution

by:
bobbit31 earned 75 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
OK. So bitblt form Bobbit could do the job.
0
 
LVL 3

Author Comment

by:dplambert
Comment Utility
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now