|
-
Feb 21st, 2003, 03:48 PM
#1
Thread Starter
PowerPoster
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
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Feb 24th, 2003, 11:35 AM
#2
PowerPoster
That's crap. You should copy the screen using BitBlt or something:
VB Code:
'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 Declare Function CreateCompatibleDC Lib "gdi32" (ByVal iDC As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal iDC As Long, _
ByVal iWidth As Long, ByVal iHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal iDC As Long, ByVal iObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal iObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal iDC As Long) As Long
Private Sub CaptureScreen()
Dim w As Long
Dim h As Long
Dim DC As Long
Dim CompatibleBitmap As Long
Dim OriginalObject As Long
'Get screen size
w = CLng(Screen.Width / Screen.TwipsPerPixelX)
h = CLng(Screen.Height / Screen.TwipsPerPixelY)
'Create buffer
DC = CreateCompatibleDC(Me.hDC)
CompatibleBitmap = CreateCompatibleBitmap(Me.hDC, w, h)
OriginalObject = SelectObject(DC, CompatibleBitmap)
'Capture fullscreen
BitBlt DC, 0, 0, w, h, GetWindowDC(GetDesktopWindow), 0, 0, vbSrcCopy
'// Here you'd copy it to your picture box or whatever
'BitBlt Me.hDC, 0, 0, w, h, DC, 0, 0, vbSrcCopy
'Release buffer
SelectObject DC, OriginalObject
DeleteObject CompatibleBitmap
DeleteDC DC
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:
'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
'Code to call:
CaptureScreen Picture1.hDC
Last edited by Fox; Feb 24th, 2003 at 11:44 AM.
-
Feb 24th, 2003, 04:23 PM
#3
Thread Starter
PowerPoster
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....
-
Feb 28th, 2003, 11:44 AM
#4
-
Mar 1st, 2003, 09:36 PM
#5
Fanatic Member
I knew you were messing around Fox. You are a cool guy.
Bird of Prey
Mr. Bald Eagle.
[img][/img]
-
Mar 6th, 2003, 10:07 AM
#6
Member
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.
-
Mar 30th, 2003, 01:57 PM
#7
Lively Member
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.
-
Apr 2nd, 2004, 10:30 AM
#8
Addicted Member
i worship any quality code produced by James Stanich
he is the man
-
Apr 2nd, 2004, 01:11 PM
#9
Thread Starter
PowerPoster
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....
-
Apr 9th, 2004, 12:03 PM
#10
Addicted Member
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.
-
May 10th, 2004, 05:12 PM
#11
yitzle,
Go to the picturebox you're tring to BitBlt to and set it's properties to Autoredraw = True
Should work then
-
May 11th, 2004, 09:35 PM
#12
Addicted Member
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.
-
May 30th, 2005, 02:26 PM
#13
Junior Member
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
-
Jul 26th, 2005, 11:22 AM
#14
Lively Member
Re: VB Snippet - Screen Capture
Fox, how can I save the image to a file?
thanks.
Naughty but Nice 
-
Oct 10th, 2007, 12:15 AM
#15
Lively Member
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??
?????
-
Oct 13th, 2007, 12:47 AM
#16
Lively Member
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
-
Nov 30th, 2010, 04:33 PM
#17
Junior Member
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()
-
Nov 30th, 2010, 05:00 PM
#18
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|