Results 1 to 18 of 18

Thread: VB Snippet - Screen Capture

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    VB Snippet - Screen Capture

    VB Code:
    1. Public Function GetScreenCapture( _
    2.    Optional ByVal FullScreen As Boolean = False) As Image
    3.  
    4.    ' Captures the current screen and returns as an Image
    5.    ' object
    6.  
    7.    Dim objSK As SendKeys
    8.  
    9.    Dim imgCapture As Image
    10.  
    11.    If FullScreen = True Then
    12.  
    13.        ' Print Screen pressed twice here as some systems
    14.  
    15.        ' grab active window "accidentally" on first run
    16.  
    17.        objSK.SendWait("{PRTSC 2}")
    18.  
    19.    Else
    20.  
    21.        objSK.SendWait("%{PRTSC}")
    22.  
    23.    End If
    24.  
    25.    Dim objData As IDataObject = Clipboard.GetDataObject()
    26.  
    27.    Return objData.GetData(DataFormats.Bitmap)
    28.  
    29. End Function
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    That's crap. You should copy the screen using BitBlt or something:

    VB Code:
    1. 'Declares
    2.     Private Declare Function GetDesktopWindow Lib "user32" () As Long
    3.     Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    4.    
    5.     Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
    6.         ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
    7.         ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
    8.         ByVal dwRop As Long) As Long
    9.    
    10.     Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal iDC As Long) As Long
    11.     Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal iDC As Long, _
    12.         ByVal iWidth As Long, ByVal iHeight As Long) As Long
    13.    
    14.     Private Declare Function SelectObject Lib "gdi32" (ByVal iDC As Long, ByVal iObject As Long) As Long
    15.     Private Declare Function DeleteObject Lib "gdi32" (ByVal iObject As Long) As Long
    16.     Private Declare Function DeleteDC Lib "gdi32" (ByVal iDC As Long) As Long
    17.  
    18. Private Sub CaptureScreen()
    19.     Dim w As Long
    20.     Dim h As Long
    21.    
    22.     Dim DC As Long
    23.     Dim CompatibleBitmap As Long
    24.     Dim OriginalObject As Long
    25.    
    26.     'Get screen size
    27.     w = CLng(Screen.Width / Screen.TwipsPerPixelX)
    28.     h = CLng(Screen.Height / Screen.TwipsPerPixelY)
    29.    
    30.     'Create buffer
    31.     DC = CreateCompatibleDC(Me.hDC)
    32.     CompatibleBitmap = CreateCompatibleBitmap(Me.hDC, w, h)
    33.     OriginalObject = SelectObject(DC, CompatibleBitmap)
    34.    
    35.     'Capture fullscreen
    36.     BitBlt DC, 0, 0, w, h, GetWindowDC(GetDesktopWindow), 0, 0, vbSrcCopy
    37.     '// Here you'd copy it to your picture box or whatever
    38.     'BitBlt Me.hDC, 0, 0, w, h, DC, 0, 0, vbSrcCopy
    39.    
    40.     'Release buffer
    41.     SelectObject DC, OriginalObject
    42.     DeleteObject CompatibleBitmap
    43.     DeleteDC DC
    44. End Sub

    Note that the code has been compressed to one function; of course you'd release the buffer somewhere else if you want to work with the captured picture. If you're using a picture box to capture the screen you don't need to copy it to a buffer first:

    VB Code:
    1. 'Declares
    2.     Private Declare Function GetDesktopWindow Lib "user32" () As Long
    3.     Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    4.    
    5.     Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
    6.         ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
    7.         ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
    8.         ByVal dwRop As Long) As Long
    9.  
    10. Private Sub CaptureScreen( iTargetDC as Long )
    11.     Dim w As Long
    12.     Dim h As Long
    13.    
    14.     'Get screen size
    15.     w = CLng(Screen.Width / Screen.TwipsPerPixelX)
    16.     h = CLng(Screen.Height / Screen.TwipsPerPixelY)
    17.    
    18.     'Capture fullscreen
    19.     BitBlt iTargetDC, 0, 0, w, h, GetWindowDC(GetDesktopWindow), 0, 0, vbSrcCopy
    20. End Sub
    21.  
    22. 'Code to call:
    23. CaptureScreen Picture1.hDC
    Last edited by Fox; Feb 24th, 2003 at 11:44 AM.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Angry Well

    Fox :

    I always enjoy constructive critism, but, if you have a better example, kindly please post without ****ting on mine. Ther is always better ways to perform something. I guess my posting my examples, at least I give you the opportunity to post SOMETHING...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  4. #4
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Sorry for that ... seeing solutions that use printscreen just hurts my old eyes That was nothing against you, just a warning for everyone trying to use the code. I tend to correct people in their ways, sorry again

  5. #5
    Fanatic Member
    Join Date
    Jun 2000
    Location
    Forest
    Posts
    545
    I knew you were messing around Fox. You are a cool guy.
    Bird of Prey

    Mr. Bald Eagle.
    [img][/img]

  6. #6
    Member
    Join Date
    Oct 2000
    Location
    Aiken, SC
    Posts
    36

    Uh huh

    Fox,

    What about Dual monitors? His Example will get both screens, yours won't.

    Joey
    You have no idea how many idiots there are among us.

    Winsock is awesome!
    If you are reading this, your a geek, just face it.

  7. #7
    Lively Member
    Join Date
    Feb 2001
    Posts
    78
    I prefer foxs code though the over one is perhaps useful if you actually want to put the screen on the clipboard. But if you just want to get a screenshot I dont think you should be changing peoples clipboards. And although you can copy it first and put it back that has bever seemed like a nice solution to me, nor has sending keys to a window unless there is no alternative.

  8. #8
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247
    i worship any quality code produced by James Stanich

    he is the man
    The "company" website My homepage: nerisoft.com
    scars heal but glory is forever

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Originally posted by Flip
    i worship any quality code produced by James Stanich

    he is the man
    Am I really?

    Thanks...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  10. #10
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153
    I tried making a program that would take a picture of the desktop and set it as the background picture of a form.
    It seems that the form must be loaded and visible for you to set its background picture, and that if you set it, then increase the forms size, it only has the screenshot for the section which was its current size.

    Can anyone help me with a program that will take a picture of the desktop BEFORE opening a form, and then set the form's picture to this screenshot?
    I'd prefer if it didn't take a pic, save to hard drive, then open it from harddrive, though if that's the only way, I'd like if someone could do even that.
    Thank you in advanced.
    Last edited by yitzle; Apr 11th, 2004 at 05:28 PM.

  11. #11
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343
    yitzle,

    Go to the picturebox you're tring to BitBlt to and set it's properties to Autoredraw = True


    Should work then

  12. #12
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153
    I'm not running Windows at the moment so I didn't check, but I'm drawing to the form background. If it doesn't have this property...
    If it does, though, thanks for the help. I'll try it out.

  13. #13
    Junior Member
    Join Date
    May 2005
    Posts
    16

    Re: VB Snippet - Screen Capture

    i simple like foxes code because it is more powerful. I am working with a game that blocks Sendkeys or Keyb_Evnet so this is very pwoerful

  14. #14
    Lively Member Tw1sted L0gic's Avatar
    Join Date
    Jan 2005
    Posts
    88

    Re: VB Snippet - Screen Capture

    Fox, how can I save the image to a file?
    thanks.
    Naughty but Nice

  15. #15
    Lively Member nilesh16782's Avatar
    Join Date
    Feb 2007
    Location
    India
    Posts
    104

    Question Re: VB Snippet - Screen Capture

    i use fox's code...
    When i use capture subroutine in timer control..
    my mouse cursor stop when this function called by timer.
    HOW CAN I STOP TO MOUSE CURSOR WHEN CAPTURING THE SCREEN??
    ?????

  16. #16
    Lively Member nilesh16782's Avatar
    Join Date
    Feb 2007
    Location
    India
    Posts
    104

    Question Re: VB Snippet - Screen Capture

    PLEASE TELL ME WHERE I M WRONG ...
    ---------------------------------------------

    'Declares
    Private Declare Function GetDesktopWindow Lib "user32" () 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 Sub CaptureScreen(iTargetDC As Long)
    Dim w As Long
    Dim h As Long
    'Get screen size
    w = CLng(Screen.Width / Screen.TwipsPerPixelX)
    h = CLng(Screen.Height / Screen.TwipsPerPixelY)
    'Capture fullscreen
    BitBlt iTargetDC, 0, 0, w, h, GetWindowDC(GetDesktopWindow), 0, 0, vbSrcCopy
    End Sub

    Private Sub Form_Load()
    CaptureScreen Me.hDC ''THIS Code WHY NOT OK
    End Sub

    Private Sub Timer1_Timer()
    'CaptureScreen Me.hDC '''BUT THIS CODE IS OK
    End Sub

  17. #17
    Junior Member
    Join Date
    Nov 2010
    Location
    United States
    Posts
    29

    Resolved Re: VB Snippet - Screen Capture

    If anyone is worried about replacing the clipboard contents you could just use something as simple as this:

    Code:
    'Have a hidden textbox somewhere on ur form and call it TB1
    
    TB1.Paste()
    GetScreenCapture()
    TB1.Copy()

  18. #18
    Junior Member
    Join Date
    Nov 2010
    Location
    United States
    Posts
    29

    Thumbs up Re: VB Snippet - Screen Capture

    Here is the simpler code:


    Code:
    Dim bounds As Rectangle
            Dim screenshot As System.Drawing.Bitmap
            Dim graph As Graphics
            bounds = Screen.PrimaryScreen.Bounds
            screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            graph = Graphics.FromImage(screenshot)
            graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
            PictureBox1.Image = screenshot
    Source:
    http://www.youtube.com/watch?v=iFkYuOuDU9c

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