TROGDOR has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Monks,

I'm having trouble using a DC to retrieve a screen capture bitmap with Win32::GUI::DIBitmap. Here's a code snipit of what I'm trying to do:

use Win32::GUI; use Win32::GUI::DIBitmap; use Win32::GuiTest qw(FindWindowLike GetWindowText); my @windows = FindWindowLike(0, qr/^search.cpan.org/); my $hdc = Win32::GUI::DC->new($windows[0]); print "hcd is $hdc\n"; print "Window name is '" . GetWindowText($windows[0]) . "'\n"; my $bmap = newFromDC Win32::GUI::DIBitmap ($hdc); print "bmap is $bmap\n";
It produces the following output:

C:\apps\Perl\dev>perl grabscreen.pl hcd is Win32::GUI::DC=HASH(0x224dbc) Window name is 'search.cpan.org: The CPAN Search Site - Microsoft Inte +rnet Explorer' bmap is
If the code were working correctly, bmap should be Win32::GUI::Bitmap=HASH(0x224de0). But it's coming back undefined. The $hdc is obviously defined, so what am I doing wrong here? Can anyone suggest a way to get more debug info from the method?

I'm aware of the other techniques to do screen captures. I prefer this technique, because it's more efficient than using the clipboard, and if I can get it to work, newFromDC will allow me to grab just a small rectangle of the window, rather than the entire window.

Thanks,
TROGDOR

Replies are listed 'Best First'.
Re: Using Win32::GUI::DIBitmap for screen capture
by critter (Acolyte) on Jul 29, 2005 at 18:34 UTC
    Hello, Think I found something for you that seems to be working. I did not test it further but I do get a value back now. Hope this helps.
    use Win32::GUI; use Win32::GUI::DIBitmap; use Win32::GuiTest qw(FindWindowLike GetWindowText); my @windows = FindWindowLike(0, qr/^search.cpan.org/); my $hdc = Win32::GUI::DC->new($windows[0]->{handle}); print "hcd is $hdc\n"; print "Window name is '" . GetWindowText($windows[0]) . "'\n"; my $bmap = newFromDC Win32::GUI::DIBitmap ($hdc); print "bmap is $bmap\n";
    This results in:
    C:\code>perl dibitmap.pl hcd is 318833948 Window name is 'search.cpan.org: The CPAN Search Site - Microsoft Inte +rnet Explo rer' bmap is Win32::GUI::DIBitmap=SCALAR(0x2251a4)
    Cheers,
    Critter
      You are getting the DC for the desktop. $windows[0]->{handle} is null.
      I've done some more testing. But just like ChrisR could not get it working yet. I could grab the entire screen to an image but not the desired window only.

      On a moment I moved from Win32::Gui::DIBitmap and tried using Win32::Screenshot to grab a window shot instead of the entire screen. The following code works with grabbing the window. The only problem it gave me was windows sometimes not being quick enough to build up the screen. This resulted in other windows being in the shot.

      It is probably not what you are looking for, but I wanted to share the code snippet anyways

      use Win32::Screenshot; use Win32::GuiTest qw(FindWindowLike SetForegroundWindow); my @windows = FindWindowLike(0,qr/^search.cpan.org/); if (defined($windows[0])) { SetForegroundWindow($windows[0]); my $image = CaptureWindow($windows[0]); $image->Write('screenshot.png'); }
      Cheers,
      Critter
Re: Using Win32::GUI::DIBitmap for screen capture
by ChrisR (Hermit) on Jul 29, 2005 at 19:21 UTC
    Yup, you've got a good one here. I haven't solved the problem by any means but I think I found an important piece of the puzzle. FindWindowLike is returning a handle to the window and not the window object. The new method of Win32::GUI::DC is expecting more.

    Here's some code that demonstrates what I think is missing:

    Chris

    Update: I found a little more info.