Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 39,945 times

Related Categories

Capture ScreenShot

This example lets you capture a picture of a specific window, the active window, or the entire screen.

Add a picturebox called picScreen, and add the code below. To use, call the GetShot procedure with the hWnd of a window. For example, to get a shot of another form in your project called frmMain, call GetShot frmMain.hwnd. Likewise, to get a shot of the whole screen, call GetShot GetDesktopWindow. (GetDesktopWindow is an API which returns the whole screen, and is declared in the code below)

Option Explicit
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
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 ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Private Type BITMAP
        bmType As Long
        bmWidth As Long
        bmHeight As Long
        bmWidthBytes As Long
        bmPlanes As Integer
        bmBitsPixel As Integer
        bmBits As Long
End Type


Private Sub GetShot(lWindowhWnd As Long)
    Dim nLeft As Long
    Dim nTop As Long
    Dim nWidth As Long
    Dim nHeight As Long
    Dim rRect As RECT
    Dim bm As BITMAP
    Dim lWindowhDC As Long
   
    Hide
    picScreen.Cls
    Set picScreen.Picture = Nothing
    GetWindowRect lWindowhWnd, rRect
    lWindowhDC = GetWindowDC(lWindowhWnd)
    '// Get coordinates
    nLeft = 0
    nTop = 0
    nWidth = rRect.Right - rRect.Left
    nHeight = rRect.Bottom - rRect.Top
    '// Blt to frm.picScreen
    BitBlt picScreen.hDC, 0, 0, nWidth, nHeight, lWindowhDC, nLeft, nTop, vbSrcCopy
    '// Del DC
    ReleaseDC lWindowhWnd, lWindowhDC
    '// set picture
    picScreen.Picture = picScreen.Image
    Show
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Thank you Very Much

    Posted by Harley_Manager on 02 Nov 2005

    Your code is the best of the internet

  • Outdated code

    Posted by Incinerator on 19 May 2005

    Is there any way this code could be re-written for use in VB.NET?

  • Help..

    Posted by mglaser on 24 Feb 2004

    Hi zzJimRaynorzz

    The problem might be that the Picturebox is still set to AutoRedraw = False. Change it to True and it should work :)

    Regards,
    Mike.

  • Help!!

    Posted by zzJimRaynorzz on 05 Oct 2003

    I like this code, but I can't seem to make it work. When it runs it shows a value for the picture before assigning it to the picScreen picture box, but there is no picture in the picScreen. Help!! ...

  • Posted by HyperHacker on 28 Mar 2003

    Nice.
    tdrice: The M was probably supposed to be a comma. Happens a lot.
    Anyone ever think of using SendKeys to press Print Screen? :D