Solved

PictureBox Screenshot

Posted on 2004-08-31
8
976 Views
Last Modified: 2008-03-04
Hello,

Is there a way to take a screenshot of the contents of a PictureBox control? I have a user control inside the PictureBox which does not exhibit a object handle (hwnd) so I cannot capture the window (object) contents this user control directly (using API). What I'm thinking of is placing the user control inside the PictureBox control and then using the PictureBox control's handle (hwnd) to somehow take a snapshot of the whole PictureBox and save it as a bitmap. Any precise solution in this scenario?

Thanks!
0
Comment
Question by:NickRice
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11948101
Microsoft has a great project in their Knowledge Base that does screenshots via API's only...

The routines in the example can:

Capture the entire contents of a form.
Capture the client area of a form.
Capture the entire screen.
Capture the active window on the screen.
Capture any portion of any window given a handle to it.
Create a Picture object from a bitmap and a palette.
Print a Picture object as large as possible on the page.

It can be found here:
http://support.microsoft.com/default.aspx?scid=kb;en-us;161299&Product=vb6

Regards,

Idle_Mind
0
 
LVL 3

Expert Comment

by:nichia
ID: 11948492
Hello!

Here is an example of how to take a screen shot of the area defined by a PictureBox and save it to a file.
You must set the picturebox AutoRedraw=True and ScaleMode=vbPixels at design time (or in code) for this to work properly.

Hope it helps. Here's the sample:


Option Explicit

Private Type POINTAPI
    x As Long
    y As Long
End Type

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 Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Sub Command1_Click()

'// Important: Set the PictureBox ScaleMode=3 vbPixels at design time.  BitBlt uses pixels.
'// Also set Picture1.AutoRedraw=True during design time.
Dim hDC As Long
Dim pt As POINTAPI
'//Get the DC of the screen
hDC = GetDC(0)
With Picture1
    '//Get the screen position of the Picturebox
    Call ClientToScreen(.hwnd, pt)
    '//Capture the screen to the picturebox
    Call BitBlt(.hDC, 0, 0, .Width, .Height, hDC, pt.x, pt.y, vbSrcCopy)
    '//convert the picturebox image to a picture object
    Set .Picture = .Image
    '//Save the picture
    Call SavePicture(.Picture, "c:/test1.bmp")
    '//Clear out the PictureBox picture
    .Picture = LoadPicture("")
End With
'//Clean up
Call ReleaseDC(0, hDC)

End Sub
0
 

Author Comment

by:NickRice
ID: 12041167
Thanks for your reply guys. I tried a number of things and most of them work, but on some computers the captured image is just a black rectangle (no snapshot). Any clues?
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

Expert Comment

by:nichia
ID: 12071702
Check the display settings on the computers which failed.

Control Panel
    Display
        Settings

Look at the color quality setting.  What is it?
0
 

Author Comment

by:NickRice
ID: 12378421
Aparently, I couldn't get a working solution to this question. The captured image came out to be a black rectangle (no snapshot) on atleast two WinXP computers I tested it on. Finally, I took another approach and used a 3rd party user control which worked fine. Thanks anyways!
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12689640
PAQed with points refunded (125)

modulo
Community Support Moderator
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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

813 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

19 Experts available now in Live!

Get 1:1 Help Now