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?


In reply to Windows screengrab with GD by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.