Important alert: (current site time 7/16/2013 4:11:22 AM EDT)
 

VB icon

Capture Screen or Active Window

Email
Submitted on: 4/23/1999
By: Dalin Nie 
Level: Not Given
User Rating: By 5 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 65045
 
     This function capture the screen or the active window of your computer Programmatically and save it to a .bmp file. This may allows you to get another machine's screen through network!!! Fully tested in VB5.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Capture Screen or Active Window
' Description:This function capture the screen or the active window of your computer
 Programmatically and save it to a .bmp file. This may allows you to get another machine's
 screen through network!!! Fully tested in VB5.
' By: Dalin Nie
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1621&lngWId=1'for details.'**************************************

'1: Declare
' This should be in the form's heneral declaration area. 
' If you do it in a module, omit the word "Private"
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _
 ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
'
'2. The Function
' You can add this to your form's code
' or you can put it in a module if the declaration is in a module
Public Function fSaveGuiToFile(ByVal theFile As String) As Boolean
' Name: fSaveGuiToFile
' Author: Dalin Nie
' Written: 4/2/99
' Purpose:
' This procedure will Capture the Screen or the active window of your Computer and Save it as 
' a .bmp file
' Input:
' theFile file Name with path, where you want the .bmp to be saved
'
' Output:
' True if successful
'
Dim lString As String
On Error goto Trap
'Check if the File Exist
 If Dir(theFile) <> "" Then Exit Function
 'To get the Entire Screen
 Call keybd_event(vbKeySnapshot, 1, 0, 0)
 'To get the Active Window
 'Call keybd_event(vbKeySnapshot, 0, 0, 0)
 
 SavePicture Clipboard.GetData(vbCFBitmap), theFile
fSaveGuiToFile = True
Exit Function
Trap:
'Error handling
MsgBox "Error Occured in fSaveGuiToFile. Error #: " & Err.Number & ", " & Err.Description
End Function
'
3. To call the function, add the code:
Call fSaveGuiToFile(yourFileNAme)
' Example: in a command1_click event add: call fSaveGuiToFile("C:\Scrn_pic.bmp")
'When you run your app, click command1, the screen will be saved in c:\scrn_pic.bmp.


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/23/1999 11:40:00 AMMatt

How do i get it to take a pcture of someones desktop over the network?!?!?!
please help i could have a lot of fun with my friends... freak them out!
(If this comment was disrespectful, please report it.)

 
4/27/1999 1:56:00 AMNeil Ramsbottom

I too would like to be able to capture
a screenshot over the network so
I can keep a watch on my brother
while he is using my Internet Connection.

I can get the code to work on a local
machine.

Matt - I would suggest that you make
a TCP/IP connection program which will
capture the screen locally(on the remote
unit) and then send the file as packets
to your unit.
(If this comment was disrespectful, please report it.)

 
4/27/1999 5:40:00 AMDalin

This is what you need to do:
1. Make a shared folder in the machine you want to watch, so that you can access files in that drive
2. Develop a little app, just put a timer, check every so often as you desire, every time the timer coms on, check the shared folder to see if the image file exist, if not, capture the screen and store there;
3. In your own macheine, look for the image in the shared folder. When you want a new look, just delete the old one. The app in the other machine will do a new one for you.

(If this comment was disrespectful, please report it.)

 
5/1/1999 7:10:00 PMkianwei

How to send the captured image back
to my computer thru winsock in VB ?
is Winsock in VB able to send
binary file or image ?
(If this comment was disrespectful, please report it.)

 
5/4/1999 5:33:00 PM(omitted)

Interesting..
(If this comment was disrespectful, please report it.)

 
5/6/1999 11:40:00 AMthx

you can probably include the machine address in the path, ie:
\\122.06.007.005\c$, as long as the process has write access to that machine. Maybe I am incorrect though... Another neat idea is to turn on another clients microphone and stream the wav back to your own machine - then u can see AND hear what they are doing - heheh!


(If this comment was disrespectful, please report it.)

 
5/11/1999 9:57:00 AMjohn

how do i call it? from a command button or a function command?
(If this comment was disrespectful, please report it.)

 
5/14/1999 4:13:00 PMLoGo

Is there a way to detrmine what color mode/color pallete (256, 16bit, etc.) that the BMP will be saved in? Becasue sending a large BMP every seconds would really lag the computers.
(If this comment was disrespectful, please report it.)

 
5/15/1999 7:02:00 AMGreg

when i used youre program in vb6 6 it said compile error and improper end function. Do you knwo how this can be fixed
(If this comment was disrespectful, please report it.)

 
5/17/1999 8:29:00 PMnattinee wittayapanya

i'm beginner use vb6 please comment web or tic or use to me thank you
(If this comment was disrespectful, please report it.)

 
5/19/1999 11:18:00 AMRaed al malhy

Cool ??
Can i get only the area i want on the form
by defining x and y .


(If this comment was disrespectful, please report it.)

 
5/22/1999 5:59:00 PMSnYpPe

Does any one know any command to turn off the monitor on a remote computer or hide/show start button and the tray clock?

Why can't I make .exe with my Visual Basic 5 - control creation (some thing like that? I can compile it to see if it would work as an .exe but that is just temporery. What is wrong? Please mail me if u can help me.

snyppe@hotmail.com
(If this comment was disrespectful, please report it.)

 
5/23/1999 7:05:00 PMHappydude

How do you call it? And what is the vbKeySnapshot?
(If this comment was disrespectful, please report it.)

 
5/29/1999 4:30:00 AMlalim

Hi,
Can U sent the project in zip file to me.
(If this comment was disrespectful, please report it.)

 
6/2/1999 12:31:00 PMashok

How does one capture a DOS window session/a client area or a user
defined area ?
(If this comment was disrespectful, please report it.)

 
6/8/1999 4:15:00 PMKurupt

hey i would like to know how to make a admin server /network way to get a screen shot from other peeps comps!!
(If this comment was disrespectful, please report it.)

 
6/24/1999 7:06:00 AMStew

To capture a dos window, you must get the handle of the active window (full screen dos window), resize the window, then use the code above to save it as a file. Then just set the window to full screen again.
(If this comment was disrespectful, please report it.)

 
6/24/1999 8:14:00 AMJay otaku

Mine doesn't work in vb6. It compile errors. what do i do?
(If this comment was disrespectful, please report it.)

 
7/15/1999 12:19:00 PMNick

I am new and I have been trying to figure out how to make this code work, but I don't know a lot about VB programming yet. I tried placing the code in a form, but I don't know how to activate the code? Do I place it in like command button click or something? And also.. where do I put the name of the file I want to save it as and the directory that I want to save it to?

Thanks
(If this comment was disrespectful, please report it.)

 
7/23/1999 8:15:00 AMSteve

Nick, you need to put the api in a module. As for those of you wondering how to send the pic over a network you need to do these steps..

save the file (which this program does I guess)
open it as a binary file
send the binary file as a message over the network to whatever computer you want to recieve it
then have the other computer recieve it and make it's picture the bitmap. It's not to hard. I will post an example as soon as I have time.
(If this comment was disrespectful, please report it.)

 
7/23/1999 8:47:00 AMJeans

HOW TO SEND THE FILE BACK TO LOCAL MACHINE??
CAN YOU PROVIDE THE CODE WHICH HOW THE WINSOCK SENDING THE FILE??
(If this comment was disrespectful, please report it.)

 
7/23/1999 4:49:00 PMPassive Matrix

If you want to add a GUI, not to hard but I thought I should post it. Make a button, textbox and a checkbox. Paste this into the form





Private Sub Command1_Click()
If Check1.Value = 1 Then
Call fSaveGuiToFileactivwin(Text1.Text)
Else
Call fSaveGuiToFile(Text1.Text)
End If
Text1.Text = App.Path & "\Shot1.Bmp"
End Sub

Private Sub Form_Load()
Text1.Text = App.Path & "\Shot.Bmp"
End Sub

(If this comment was disrespectful, please report it.)

 
7/23/1999 5:45:00 PMPasive Matrix

Does anyone know how to have the program take a picture when you press a key? Like when you minimize the window, hilight Internet Explorer and press f12 it takes a screenshot?
(If this comment was disrespectful, please report it.)

 
7/23/1999 8:41:00 PMYoesoef

I've tried to send the file through winsock. It's binary file (1 M).
But the client can't receive all the file. The client only receive less than 50 K.
How solving the problem ???

These are the codes:

Private Sub Command1_Click()
Dim Ff as Integer, By() as Byte
Ff = FreeFile
Open "C:\WINDOWS\Shot.bmp" For Binary as #Ff
ReDim By(1 to LOF(Ff))
Get #Ff, , By
Close #Ff
Winsock1.SendData By
DoEvents
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim Ff as Integer, By() as Byte
Ff = FreeFile
Winsock1.GetData By, vbArray + vbByte
DoEvents
Open "C:\WINDOWS\Shot1.bmp" For Binary as #Ff
Put #Ff, , By
Close #Ff
End Sub

(If this comment was disrespectful, please report it.)

 
7/23/1999 9:17:00 PMKevino

Great code. I would like to point out that it doesn't seem to work if a file with the same name already exists. You should make sure that the old bitmap file is deleted first before attempting to overwrite the file.
(If this comment was disrespectful, please report it.)

 
7/23/1999 10:07:00 PMKevino

Duh...forget my last comment
(If this comment was disrespectful, please report it.)

 
7/26/1999 7:05:00 AMSteve

NOw use this to go a step further with a network. Use GetCursorPos to get your cursor's position, then use SetCursorPos on their computer to set their cursors position to yours. Using winsock to send data. Then when your done with that find out some api that allows you to cause a double click or single click to occur and use it to open programs. I am working on this, however am stuck on the clicking part. Basicly I want to make a program for troubleshooting over a network. It already uses sendkeys so you can type into things, etc, you click on a text box then just type on the keyboard and it sends whatever you type, however it isn't working yet as far as that goes because of the mouse. I know the sendkeys work though because if the textbox of the program has focus you can type into it. Other than that, it is working great. If anyone has any ideas on how to do the click and double click please email me.
(If this comment was disrespectful, please report it.)

 
7/28/1999 12:10:00 PMRon

This code works good, but if there is somthing in the clipboard first it will just be a blank picture. For some reason I can't clipboard.clear it out either.

This works real well also
Use the same header function

Private Sub Form_Load()
Dim x As Integer
'Entire Screen
Call keybd_event(vbKeySnapshot, 1, 0, 0)
Image1.Picture = Clipboard.GetData(vbCFBitmap)
Me.Left = 0
Me.Top = 0
Me.Width = Screen.Width
Me.Height = Screen.Height
End Sub
(If this comment was disrespectful, please report it.)

 
8/11/1999 10:34:00 AMAaronC

Not much of a solution for the clipboard problem, but I have noticed that if you call clipboard.clear several times from an outside proc then it clears it out correctly.
(If this comment was disrespectful, please report it.)

 
8/14/1999 7:21:00 PMThe DonutMan

I have an idea for the clipboard problem. Maybe if a DoEvents command is added after Clipboard.Clear, then it may clear correctly. That would explain why multiple clears works while a single one does not. To all of you who are wondering about getting a screenshot across a network, you must first learn to use the Winsock control. Once you've learned that, you have to install a copy of the program on both computers, and then have one of the computers periodically send a screen shot back to the other.
(If this comment was disrespectful, please report it.)

 
8/16/1999 9:13:00 PMDaniel

Ummm 1 prob, this savepiccture method saves a file over 1 meg big

any solutions
(If this comment was disrespectful, please report it.)

 
8/18/1999 1:25:00 AMDan

Inorder to send a image or any binary file over a TCP\IP connection you will have to somehow compress it. Also, sending image files, especialy clear and well taken ones such as this will take some time. Best would be to write a simple WINSOCK API application of a client\server. But to send winsock files is complex. I am currently developing a internet communications program with file transfer technology, but this is being done in Pascal and C++ so posting it here wont do much good... Best of luck
(If this comment was disrespectful, please report it.)

 
8/23/1999 4:00:00 AMMike

Dan: You don't have to compress any file to be able to send it via TCP/IP, it just takes longer!. Using the VB winsock control to send data is very easy, a 5 minute job.
Yosoef: You have to split the file into small chunks (i.e. less than 50k) and send it one chunk at a time. The file will have to be pieced together at the other end.

(If this comment was disrespectful, please report it.)

 
8/23/1999 4:08:00 AMMike

Everyone seems to be very keen to do remote access to another computer...
Go and download Timbuktu from http://www.netopia.com/software/tb2/win/ you can observe and control a remote PC over a TCP network. (evaluation copy BTW)
(If this comment was disrespectful, please report it.)

 
8/30/1999 6:09:00 PMBillyTBashed5446

You Use WINSOCK PEOPLE If need me to send you a Sorce Code TOO BAD! But i will happily make it into an exe if you trust me!!!! -BILLY
(If this comment was disrespectful, please report it.)

 
9/12/1999 2:06:00 AMme

is this all u guys do with vb is try to figure out how to make ur own netbus¿ !Lamers!
(If this comment was disrespectful, please report it.)

 
9/13/1999 3:29:00 PMPaper-Maker

For all of you asking about the error "380, Invalid Properity Value", it is because you are not declaring your file name. Insert this code like in a command button or something:

Dim yourFileNAme As String
yourFileNAme = "C:\ss.bmp"
Call fSaveGuiToFile(yourFileNAme)

I am currently working on some sorta netbus type thing, with network and all, so e-mail me in a month and you can see if I have finished it or not.
(If this comment was disrespectful, please report it.)

 
9/18/1999 1:45:00 PMunkown

Anyone know how to create an app like Screen Recorder which saves every thing you do to as a avi file?
(If this comment was disrespectful, please report it.)

 
10/30/1999 3:34:00 AMLAW Chun Wai

How am i supposed to write a program which capture video from a USB camera?
(If this comment was disrespectful, please report it.)

 
11/28/1999 9:12:00 AMX24™

Is it possible to capture only a part of a form, let's say a picturebox or a frame?
(If this comment was disrespectful, please report it.)

 
12/10/1999 12:03:00 PMnick

Hey I'm writing a screensaver in vb 5.0 I have it mostly done but I'm having problems getting the images to flip what code should I use to get it to flip correctly on like a 10 second timer? thanks a lot I'm new at this vb stuff I have been doing it for all of two days now so any additional thoughts on how to make a good screensaver in vb would be greatly appreciated
(If this comment was disrespectful, please report it.)

 
1/20/2000 7:32:27 PMjamie

i will make a fully functioning screen capturer for networks and internet.
if you want it email me at
wa_master@hotmail.com
(If this comment was disrespectful, please report it.)

 
1/24/2000 11:21:28 PMSpoofer

You make a tcp/ip connection or use winsock. Then make a command in the program to sent to a user to send the file to you over the internet. Then make another tcp/ip or winsock program to execute the command and accept the file

(If this comment was disrespectful, please report it.)

 
11/4/2000 12:57:19 AMExeat

Ok... just browsing down these messages I noticed people wanting to screen capture over networks to freak people out and stuff like that.

Well... you can all just download Sub7 which will basically have the victim in your control, this program can basically do anything you want to their computer!

The main drawback of this is that they have to have ther server.exe file on their computer and they have to run it.

I'm working on a program that will do this for you, but until then... you'll have to con them into thinking they need what you are sending them.

If you have decided to get this program, download it for free off http://subseven.slak.org/main.html oh yeah, you will be using this at your own risk because it is illegal unless you have permission from the owner.

-Laterz
(If this comment was disrespectful, please report it.)

 
6/19/2001 4:21:45 PMrandom

what you want is netbus...eheheh

(If this comment was disrespectful, please report it.)

 
3/28/2002 7:32:07 PMThFabba

well, u might have seen that i voted with 'poor' for this code but i wont do that without explaining why of course... at first i think pressing the print screen key is a very strange (discouraged) way for a program to do a screenshot since there are a couple of functions, like BitBlt, which one can use easily for that purpose. There are some good examples here for capturing screen or active window or only parts of those. Also the code could be better commented since there was even a question like 'Can I do this with specific X and Y coordinates?' which is not possible with this code (except if u extract a part of the image from clipboard) meaning that unexperienced people cannot understand what this code really does (although they should know how to find keybd_event in msdn :\ ). continues below...
(If this comment was disrespectful, please report it.)

 
3/28/2002 7:32:25 PMThFabba

Another thing is that i wouldnt suggest using keybd_event for simulating a keypress since that function has been superseded by the SendInput function which will be more effective. so i just think this code is not a good way to take a screen shot...
(If this comment was disrespectful, please report it.)

 
6/2/2003 1:54:00 PM

Could this code be inserted into VBA, like Office Access module? What else should I add to make it work in VBA? Because I always fail to make the function work
(If this comment was disrespectful, please report it.)

 
6/2/2003 1:56:21 PM

How can I make this code work in Office VBA? Because I always fail to make the line work correctly. "SavePicture Clipboard.GetData(vbCFBitmap), theFile"
Thanks in advance.
(If this comment was disrespectful, please report it.)

 
8/15/2003 11:41:54 AM

it was a great code can you send me complete code for this
(If this comment was disrespectful, please report it.)

 
5/5/2004 5:50:42 AM

How do I get the program to capture the window of another program?
(If this comment was disrespectful, please report it.)

 
1/5/2005 4:50:40 AM

how to capture another computer dekstop,hw to apply in this code......plz help me thank u
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.