Results 1 to 6 of 6

Thread: how can i take a screenshot of the computer with VB?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746

    how can i take a screenshot of the computer with VB?

    How can I take a screen shot of the computer my VB program is runnning on?

  2. #2

  3. #3
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Or if you like to do things the hard way like me.....

    VB Code:
    1. Private Declare Function EmptyClipboard Lib "user32" () As Long
    2. Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
    3. Private Declare Function CloseClipboard Lib "user32" () As Long
    4. Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
    5. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    6. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    7. 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
    8. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    9. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    10. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    11. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    12. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    13.  
    14. Private Const CF_BITMAP = 2
    15.  
    16. Public Function CopyScreenToClipboard() As Long
    17.  Dim lngScreenDC As Long
    18.  Dim lngScreenH As Long, lngScreenW As Long
    19.  Dim lngDC As Long
    20.  Dim lngBMP As Long, lngOrigBMP As Long
    21.  
    22.  ' get a handle to the screen
    23.  lngScreenDC = GetDC(GetDesktopWindow)
    24.  ' get the dimensions of the screen in pixels
    25.  lngScreenH = Screen.Height \ Screen.TwipsPerPixelY
    26.  lngScreenW = Screen.Width \ Screen.TwipsPerPixelX
    27.  
    28.  ' create a dc to temporarily hold the screen bmp
    29.  lngDC = CreateCompatibleDC(lngScreenDC)
    30.  
    31.  ' create the bitmap in memory to hold picture of the screen
    32.  lngBMP = CreateCompatibleBitmap(lngScreenDC, lngScreenW, lngScreenH)
    33.  
    34.  ' put the BMP created into the DC created
    35.  lngOrigBMP = SelectObject(lngDC, lngBMP)
    36.  
    37.  ' blt the current state of the screen to the DC
    38.  BitBlt lngDC, 0, 0, lngScreenW, lngScreenH, lngScreenDC, 0, 0, vbSrcCopy
    39.  
    40.     'open the clipboard
    41.     If OpenClipboard(Me.hwnd) Then
    42.        
    43.          ' clear the clipboard of current data
    44.         If EmptyClipboard Then
    45.        
    46.             ' put the bitmap in clipboard
    47.             SetClipboardData CF_BITMAP, lngBMP
    48.  
    49.             ' don't need to be thw owner of the clipboard anymore so let it go
    50.             CloseClipboard
    51.            
    52.         Else
    53.        
    54.             MsgBox "Error saving data to clipboard!"
    55.        
    56.         End If
    57.        
    58.     Else
    59.  
    60.         MsgBox "Error saving data to clipboard!"
    61.  
    62.     End If
    63.  
    64.  
    65.  ' free memory
    66.  SelectObject lngDC, lngOrigBMP
    67.  DeleteDC lngDC
    68.    
    69. End Function
    70.  
    71. Private Sub Command1_Click()
    72.     CopyScreenToClipboard
    73. End Sub

    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    i got a error "invalid use of me keywork." and i need it to copy the screen shot to a file on the desktop or somethin so it can easily be sent to my tech support without any anoying messages.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    k i got rid of the error but is there a way i could transfer it to a .gif file or something that can be easily transfered?

  6. #6
    You'll have to use a third-party control from either http://www.activex.com/ or http://www.planetsourcecode.com/ . Intrinsically, VB cannot save files in JPEG or GIF format.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width