Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Screen saved to bitmap file 2

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
I have a rather unique requirement that I need to code.&nbsp;&nbsp;I need to be able to be able to capture the contents of the screen and save it to a bitmap file in an automated fashion (i.e. a function call).&nbsp;&nbsp;I'd prefer to use Visual Basic.<br><br>The logic would work something like this:<br><br>If flag is set then:<br><br>1) print screen (copies contents of screen to clipboard<br>2) save clipboard bitmap contents to filename.<br><br>I've gone through some of my manuals but can't find anything that would clearly and easily do the trick.<br><br>If another tek-tip forum would make more sense to post this question please let me know....<br><br>jerry
 
Your question could probably be addressed in the VB General Discussion but, since you're here... I think this is what you are looking for.<br><br>Create a form with two picture boxes and a timer control on it (Picture1, Picture2 and Timer1). Place the following code in the Timer control (set its Interval property to 1000 - one second - or however often you want to check for new graphics on the clipboard). Run the program, minimize it (to get it out of sight) and then press the Print Screen key. The program will create a file called Pic0.bmp in the current directory. Subsequent presses of Print Screen will create Pic1.bmp, Pic2.bmp, and so on, until you end the program.<br><FONT FACE=monospace><b><br>Private Static Sub Timer1_Timer()<br>On Error Resume Next<br>Picture1.Picture = Clipboard.GetData(vbCFBitmap)<br>If Picture1.Picture &lt;&gt; Picture2.Picture Then<br>&nbsp;&nbsp;&nbsp;&nbsp;SavePicture Picture1.Picture, &quot;Pic&quot; & Trim$(Str(PicCounter)) & &quot;.BMP&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;Picture2.Picture = Picture1.Picture<br>&nbsp;&nbsp;&nbsp;&nbsp;PicCounter = PicCounter + 0.5<br>&nbsp;&nbsp;&nbsp;&nbsp;Clipboard.Clear<br>End If<br></font></b><br><br>You may notice the &quot;On Error Resume Next&quot; and the weird fractional increment of PicCounter. Explanation? I'm a sloppy programmer who likes to keep my routines small (hate typing a lot) and I really don't mind creating an app error (or two) if it helps to make the program short and sweet.<br><br>One of the gurus on this site will probably show you a way to do this with less code and without the error deferral. The more the merrier! &lt;grin&gt;<br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Have just completed writing an activeX control allowing you to capture either the full desktop or the active Client window and save this to a file <br><br>if you would like me to mail it to you please provide your address <br><br>
 
Vince,<br><br>Yes,&nbsp;&nbsp;I'm very much interested.&nbsp;&nbsp;could you please mail it to:<br><br><A HREF="mailto:jerrysussman@att.net">jerrysussman@att.net</A><br><br>thanks,<br><br>jerry
 
To:&nbsp;&nbsp;ALT255,<br><br>I've been playing around with the code you referenced.&nbsp;&nbsp;I'm new to visual basic and the code help me learn some topics I had not run into before.&nbsp;&nbsp;&nbsp;The code of course works fine.&nbsp;&nbsp;One question though....<br><br>Can you provide me with the visual basic statement(s) that are required to get the image on the screen copied to the clipboard.&nbsp;&nbsp;I need to have the code setup such that whenever the program is executed it will automatically copy what is on the display to the clipboard.<br><br>I've gone through massive reference books and the help feature but can't find a simple Visual BASIC command(s) that will do the same basic function as hitting the &quot;ALT-Printscreen&quot; key on the keyboard.<br><br>From there I believe I can get the bitmap and then save to a file.<br><br>thanks,<br><br>Jerry
 
I can't take credit for this code but it seems to to the job pretty well:<br><br>Addd these to a module...<br><FONT FACE=monospace><b><br>Private Declare Sub keybd_event Lib &quot;user32&quot; _ <br>&nbsp;&nbsp;&nbsp;&nbsp;(ByVal bVk As Byte, ByVal bScan As Byte, _ <br>&nbsp;&nbsp;&nbsp;&nbsp;ByVal dwFlags As Long, ByVal dwExtraInfo As Long) <br><br>Private Const VK_MENU = &H12 <br>Private Const VK_SNAPSHOT = &H2C <br>Private Const KEYEVENTF_KEYUP = &H2 <br><br>Public Function SaveScreen(sFile As String) As Boolean <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim sPic As IPictureDisp <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo Errhandler <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Clipboard.Clear <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event VK_MENU, 0, 0, 0 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoEvents <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event VK_SNAPSHOT, 1, 0, 0 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoEvents <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoEvents <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set sPic = Clipboard.GetData(0) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SavePicture sPic, sFile <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Clipboard.Clear <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set sPic = Nothing <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SaveDesktop = True <br>Errhandler: <br>End Function <br></font></b><br>Call the routine like this....<br><FONT FACE=monospace><b><br>Private Sub Command1_Click() <br>&nbsp;&nbsp;&nbsp;&nbsp;SaveScreen &quot;My.bmp&quot; <br>End Sub <br></font></b><br>Cheers!<br><img src=http:\\vorpalcom.com\images\gears.gif> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
cool.... nice one craig <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
The above suggestions have all worked great. I've learned a lot and the code that I've implemented worked fine.....with one interesting exception.

The 'snap-shots' work great on standard windows. The exception is when I have a window open that has a video image.

For example, let's say I have a graphics card with a TV Receiver and I have the active window open that has a tv video image coming through. My code needs to grab a 'snapshot' of the video automatically whenever I issue a software 'trigger'.

The code above works great for standard windows, but for windows that have a video image the bitmap is just all blank.

I'm new to Visual Basic but am learning quickly. Nobody that is local to me has any idea how to attack this one....

help? Anyone have an idea? I suspect that video images are handled differently.

/jerry
 
Jerry, the code shown above should work properly if the video output is in a normal window. For captures in a maximized window, you should check the graphics card documentation. Most of those cards, I believe, ship with a utility that allows screen captures. The solution may be as simple as using SendKeys to issue a hot-key to the utility. The software for the card may even allow a limited API.

If such a utility is not available you might consider contacting the manufacturer, explain what you intend to do and see what happens. Some manufacturers understand the need for partnership with independent software developers. The idea here is that the availablity of add-ons enhance the marketability of almost any product.
 
Jerry,

You will never be able to take a screen shot of that video. Why? Because it's simply not there. The video is superimposed onto your video signal right before being sent to your monitor. Windows never &quot;sees&quot; it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top