VB Snippet - Screen Capture
VB Code:
Public Function GetScreenCapture( _
Optional ByVal FullScreen As Boolean = False) As Image
' Captures the current screen and returns as an Image
' object
Dim objSK As SendKeys
Dim imgCapture As Image
If FullScreen = True Then
' Print Screen pressed twice here as some systems
' grab active window "accidentally" on first run
objSK.SendWait("{PRTSC 2}")
Else
objSK.SendWait("%{PRTSC}")
End If
Dim objData As IDataObject = Clipboard.GetDataObject()
Return objData.GetData(DataFormats.Bitmap)
End Function
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
Re: VB Snippet - Screen Capture
Fox, how can I save the image to a file?
thanks.
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??
?????
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
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()
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