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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Windows screengrab with GD
by Anonymous Monk on Dec 29, 2014 at 23:36 UTC | |
by BrowserUk (Patriarch) on Dec 30, 2014 at 00:33 UTC | |
by BrowserUk (Patriarch) on Dec 30, 2014 at 11:22 UTC | |
by Anonymous Monk on Dec 30, 2014 at 12:57 UTC | |
by BrowserUk (Patriarch) on Dec 30, 2014 at 13:37 UTC | |
by Anonymous Monk on Dec 30, 2014 at 01:42 UTC | |
|
Re: Windows screengrab with GD
by Anonymous Monk on Dec 30, 2014 at 01:08 UTC | |
by BrowserUk (Patriarch) on Dec 30, 2014 at 01:21 UTC | |
by BrowserUk (Patriarch) on Dec 30, 2014 at 01:23 UTC |