Created
November 5, 2012 01:57
-
-
Save kubrick06010/4014844 to your computer and use it in GitHub Desktop.
A simple method of doing a screen capture on Windows. For a more flexible approach see Win32::Screenshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl -w | |
| use strict; | |
| use Win32::Clipboard; | |
| use Win32::GuiTest 'SendKeys'; | |
| # Send "Print Screen" key to Windows. | |
| SendKeys('{PRTSCR}'); | |
| # Get the image from the clipboard. | |
| my $screen = Win32::Clipboard::GetBitmap() | |
| or die "No image captured: $!\n"; | |
| # Print the image to a file. | |
| open BITMAP, "> screen.bmp" or die "Couldn't open bitmap file: $!\n | |
| +"; | |
| binmode BITMAP; | |
| print BITMAP $screen; | |
| close BITMAP; | |
| __END__ | |
| # | |
| # Or to capture a specific Window: | |
| # | |
| #!/usr/bin/perl -w | |
| use strict; | |
| use Win32::Clipboard; | |
| use Win32::GuiTest qw(FindWindowLike SetForegroundWindow SendKeys); | |
| # Find a specific Window | |
| my @windows = FindWindowLike(0, qr/^Microsoft Excel/, qr/^XLMAIN$/); | |
| for (@windows) { | |
| SetForegroundWindow($_); | |
| SendKeys('%{PRTSCR}'); # Alt Print Screen | |
| } | |
| # Get the image from the clipboard. | |
| my $screen = Win32::Clipboard::GetBitmap() | |
| or die "No image captured: $!\n"; | |
| # Print the image to a file. | |
| open BITMAP, "> screen.bmp" or die "Couldn't open bitmap file: $!\n | |
| +"; | |
| binmode BITMAP; | |
| print BITMAP $screen; | |
| close BITMAP; | |
| __END__ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by jmcnamara
on May 20, 2004 at 20:50 UTC ( #355073=snippet: print w/ replies, xml ) at http://www.perlmonks.org/?node_id=355073