[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1087
  • Last Modified:

PictureBox Screenshot

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
NickRice
Asked:
NickRice
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
nichiaCommented:
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
 
NickRiceAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nichiaCommented:
Check the display settings on the computers which failed.

Control Panel
    Display
        Settings

Look at the color quality setting.  What is it?
0
 
NickRiceAuthor Commented:
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
 
moduloCommented:
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now