Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
"be consistent"
 
PerlMonks  

Windows screengrab with GD

by BrowserUk (Pope)
on May 28, 2012 at 12:29 UTC ( #972818=CUFP: print w/ replies, xml ) Need Help??

As posted, takes some text as an argument which is used to bring the wanted window to the front and then an Alt-printscreen is issued. The Windows bitmap is grabbed from the clipboard, interpreted and used to construct a GD Image.

It can then be further manipulated with GD or written to disk in any of the GD support formats: PNG, JPEG, GIF etc.

#! perl -slw use strict; use Data::Dump qw[ pp ]; use GD; use Win32::GuiTest qw[FindWindowLike SetForegroundWindow SendKeys]; use Win32::Clipboard; use constant { FSIG => 0, FSIZE => 1, FOFFSET => 4, HSIZE => 5, WIDTH => 6, HEIGHT => 7, PLANES => 8, BITSPERPIXEL => 9, COMP => 10, ISIZE => 11, }; sub bmp2gd { my $bitmap = shift; my @info = unpack 'A2ISSI IIISSII', $bitmap; my $gd = GD::Image->new( @info[ WIDTH, HEIGHT ], 1 ); $gd->alphaBlending( 0 ); my $offset = $info[FOFFSET]; for my $y ( 0 .. $info[HEIGHT]-1 ) { my @rgbs = unpack 'V*', substr( $bitmap, $offset+ ( $info[WIDTH]*$y*4 ), $info[WIDTH] *4 ); $gd->setPixel( $_, $info[HEIGHT]-$y, $rgbs[ $_ ] ) for 0 .. $i +nfo[WIDTH]-1; } return $gd; } my $clip = Win32::Clipboard->new(); my @windows = FindWindowLike( 0, "^$ARGV[ 0 ]" ); #Find it die "Couldn't find window '$ARGV[ 0 ]" unless @windows; SetForegroundWindow( $windows[0] ); sleep 1; SendKeys("%{PRTSCR}"); my $fname = "$ARGV[ 0 ]-@{[ time() ]}.png"; print $fname; if( my $bitmap = $clip->GetBitmap() ) { my $gd = bmp2gd( $bitmap ); open PNG, '>:raw', $fname or die $!; print PNG $gd->png; close PNG; system 1, $fname; } else { die "Couldn't get bitmap from clipboard"; }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Comment on Windows screengrab with GD
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://972818]
Approved by marto
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (18)
As of 2013-05-23 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (486 votes), past polls