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?

Replies are listed 'Best First'.
Re: Windows screengrab with GD
by Anonymous Monk on Dec 29, 2014 at 23:36 UTC
    Hi :)

    Thank you for this great tool, I use it very often. But I found very strange issue, while using it.

    On some images it replace white color with black. I was trying to understand reason by myself, but I can't.

    I found such image and make crop from it. To reproduce an issue open original image in any viewer and make screenshot of it.

    Both images here (will be there for 1 week): http://dropmefiles.com/KOAgy

    Thanks in advance for any help!

      To reproduce an issue open original image in any viewer and make screenshot of it. Both images here (will be there for 1 week): http://dropmefiles.com/KOAgy

      Hm. I followed that link and all I saw were two completely unrelated images; A & B.

      Of course, I followed your link with JS disabled, and from a quick peek at the source, the page is heavy with JS -- so that might be the reason -- but, I'm no mug.

      If you want the issue addressed; post a direct (non-redirected, non-JS dependant) link to the two images and I might try to reproduce the problem.


      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.

      Okay. I've tried the before image you posted and a bunch of other jpgs, pngs and gifs and I cannot reproduce the problem here.

      The only possibility that comes to mind is that the original image was an 8-bit color image and the process of converting that, first to a 24-bpp MS bitmap, and then to 24-bpp PNG might have introduced some artifacts.

      Unless the original image is available, in its original form, on the net somewhere, I cannot see any way to progress this?


      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.
        Thank you!

        Well, I posted crop because I have exactly same issue with it as with full image (that's how I got second image from my sample). Also I download uploaded crop image before posting it here and compared it with my to make sure hosting site not altered it.

        So, if you can't reproduce that issue - you will not able to reproduce it with full one as well or with any other images I had. That helped me a lot. Looks like it's something with my Perl/GD or maybe Aero theme.

        Thank you very much and sorry for disturb. :)

        I temporary use workaround, by replacing Perl conversation with bmp2png tool. If I'll found reason for my issue, I'll post an update.

      Sure, tyvm!

      I planned to post version info, but was unsure if that will be helpful, since issue very specific:

      Windows 7 (x32): Microsoft Windows Version 6.1.7601

      GD: 2.53

      Strawberry Perl:

      Summary of my perl5 (revision 5 version 20 subversion 0) configuration:

      Platform:

      osname=MSWin32, osvers=6.2, archname=MSWin32-x86-multi-thread-64int

      uname='Win32 strawberry-perl 5.20.0.1 #1 Fri May 30 23:25:11 2014 i386'

Re: Windows screengrab with GD
by Anonymous Monk on Dec 30, 2014 at 01:08 UTC
    Oh, really sorry it's cropped it in weird way. There is link to download both files, but no idea if it will work without JS.

    I'll reupload at site you using, tyvm!

    Test image crop - http://imgbin.org/images/21556.png

    Result with visible issues - http://imgbin.org/images/21557.png

    P.S. Can't reply - permissions denied error.

      I'll reupload at site you using, tyvm!

      Thanks. I got both images and see the problem.

      It's late here, so I'll take a look tomorrow and get back to you.

      (Also; could you post the versions of Perl & GD you are using; plus your OS/version.) Thanks.


      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.
      I'll reupload at site you using, tyvm!

      Thanks. I got both images and see the problem.

      It's late here, so I'll take a look tomorrow and get back to you.

      (Also; could you post the versions of Perl & GD you are using; plus your OS/version.) Thanks.

      P.S. Can't reply - permissions denied error.

      I here you. I've reported the problem several times, and they-that-can choose-not-to deal with the problem. Why? Your guess is (probably) as good as mine.


      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.