Quick Search for:  in language:    
VB5,function,capture,screen,active,window,you
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Visual Basic Stats

 Code: 3,011,557. lines
 Jobs: 115. postings

 How to support the site

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for Visual Basic.
AniViewer
By Jerrame Hertz on 6/30


Click here to see a screenshot of this code!Raw Packet Sniffer
By Coding Genius on 6/30

(Screen Shot)

Check the support of a record set
By Freebug on 6/30


B++ Builder - VB without runtimes
By Anthonius on 6/30


Mr Blonde - Chat Program
By Mr Blonde on 6/30


MSN Messenger Status Detector
By Ryan Cain on 6/30


MSN advanced
By alias1990 on 6/30


MSN Messenger advanced
By alias1990 on 6/30


Locate Database
By Erica Ziegler-Roberts on 6/30


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Capture Screen or Active Window

Print
Email
 

Submitted on: 4/23/1999
By: Dalin Nie 
Level: Not Given
User Rating: By 108 Users
Compatibility:VB 4.0 (32-bit), VB 5.0, VB 6.0

Users have accessed this code 23561 times.
 
 
     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 comp
'     uter
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://w
'     ww.Planet-Source-Code.com/vb/scripts/Sho
'     wCode.asp?txtCodeId=1621&lngWId;=1'for details.'**************************************

'1: Declare
' This should be in the form's heneral d
'     eclaration area. 
' If you do it in a module, omit the wor
'     d "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 d
    '     eclaration 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 an ' d 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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:Matt
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/27/1999 1:56:00 AM:Neil 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/27/1999 5:40:00 AM:Dalin
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/1/1999 7:10:00 PM:kianwei
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 ?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/4/1999 5:33:00 PM:(omitted)
Interesting..
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/6/1999 11:40:00 AM:thx
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/11/1999 9:57:00 AM:john
how do i call it? from a command button 
or a function command?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/14/1999 4:13:00 PM:LoGo
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/15/1999 7:02:00 AM:Greg
when i used youre program in vb6 6 it 
said compile error and improper end 
function. Do you knwo how this can be 
fixed
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/17/1999 8:29:00 PM:nattinee wittayapanya
i'm beginner use vb6 please comment web 
or tic or use to    me thank you
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/19/1999 11:18:00 AM:Raed al malhy
Cool ??
Can i get only the area i want 
on the form
by defining x and y .
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/22/1999 5:59:00 PM:SnYpPe
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/23/1999 7:05:00 PM:Happydude
How do you call it?  And what is the 
vbKeySnapshot?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/29/1999 4:30:00 AM:lalim
Hi,
Can U sent the project in zip file 
to me.  
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/2/1999 12:31:00 PM:ashok
How does one capture a DOS window 
session/a client area or a 
user
defined area ?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/8/1999 4:15:00 PM:Kurupt
hey i would like to know how to make a 
admin server /network way to get a 
screen shot from other peeps comps!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/24/1999 7:06:00 AM:Stew
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/24/1999 8:14:00 AM:Jay otaku
Mine doesn't work in vb6.  It compile 
errors. what do i do?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/15/1999 12:19:00 PM:Nick
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/1999 8:15:00 AM:Steve
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/1999 8:47:00 AM:Jeans
HOW TO SEND THE FILE BACK TO LOCAL 
MACHINE??
CAN YOU PROVIDE THE CODE 
WHICH HOW THE WINSOCK SENDING THE FILE??
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/1999 4:49:00 PM:Passive 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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/1999 5:45:00 PM:Pasive 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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/1999 8:41:00 PM:Yoesoef
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/1999 9:17:00 PM:Kevino
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/1999 10:07:00 PM:Kevino
Duh...forget my last comment
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/26/1999 7:05:00 AM:Steve
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.  
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/28/1999 12:10:00 PM:Ron
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/11/1999 10:34:00 AM:AaronC
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/14/1999 7:21:00 PM:The 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/16/1999 9:13:00 PM:Daniel
Ummm 1 prob, this savepiccture method 
saves a file over 1 meg big
any 
solutions
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/18/1999 1:25:00 AM:Dan
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/23/1999 4:00:00 AM:Mike
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/23/1999 4:08:00 AM:Mike
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)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/30/1999 6:09:00 PM:BillyTBashed5446
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/12/1999 2:06:00 AM:me
is this all u guys do with vb is try to 
figure out how to make ur own netbus¿  
!Lamers!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/13/1999 3:29:00 PM:Paper-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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/18/1999 1:45:00 PM:unkown
Anyone know how to create an app like 
Screen Recorder which saves every thing 
you do to as a avi file?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/30/1999 3:34:00 AM:LAW Chun Wai
How am i supposed to write a program 
which capture video from a USB camera?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/28/1999 9:12:00 AM:X24™
Is it possible to capture only a part 
of a form, let's say a picturebox or a 
frame?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/10/1999 12:03:00 PM:nick
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/20/2000 7:32:27 PM:jamie
i will make a fully functioning screen 
capturer for networks and internet.
if 
you want it email me at 
wa_master@hotmail.com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/24/2000 11:21:28 PM:Spoofer
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/4/2000 12:57:19 AM:Exeat
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/19/2001 4:21:45 PM:random
what you want is netbus...eheheh
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/28/2002 7:32:07 PM:ThFabba
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...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/28/2002 7:32:25 PM:ThFabba
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...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Visual Basic Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.