Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Screen Capture Module via wxPerl

by jmlynesjr (Deacon)
on Oct 23, 2012 at 17:56 UTC ( [id://1000505]=CUFP: print w/replies, xml ) Need Help??

The Screen Capture wxPerl script is now in modular form. My first try at creating a (very simple) module and using POD.

James

Update-added to POD sections

Module wxScreenCapture
use strict; use warnings; package WxScreenCapture; use parent qw(Exporter); our @EXPORT = qw(take_screenshot file_entry_dialog_simple file_entry_d +ialog_standard); use Wx qw(:everything); use Wx::Event qw( EVT_PAINT ); =head2 # # Copy the screen to the output file-similar to wxBook pg 139 example. # =cut sub take_screenshot { my($self, $file ) = @_; $self->Refresh; # without Refresh and Updat +e $self->Update; # the underlying window is c +aptured my $screen = Wx::ScreenDC->new(); my( $x, $y) = $screen->GetSizeWH(); my $bitmap = Wx::Bitmap->new($x,$y,-1); my $memory = Wx::MemoryDC->new(); $memory->SelectObject( $bitmap ); $memory->Blit(0,0,$x,$y, $screen, 0, 0); # copy the scr +een to the bitmap $bitmap->SaveFile( $file , wxBITMAP_TYPE_BMP ) ; # copy the + bitmap to the output file } =head2 # # Ask for the output filename - simple text entry dialog # =cut sub file_entry_dialog_simple { my( $self ) = @_; my $textvalue = "capture.bmp"; my $dialog = Wx::TextEntryDialog->new ( $self, "Enter the Screen Capture output filename\n(Cancel will u +se the default filename shown below)\n", "Screen Capture Output File Entry", $textvalue, wxOK | wxCANCEL ); if( $dialog->ShowModal == wxID_OK ) { $textvalue = $dialog->GetValue; } $dialog->Destroy; return $textvalue; } =head2 # # Ask for the output filename - standard file dialog # =cut sub file_entry_dialog_standard { my ( $self ) = @_; my $caption = "Choose a Screen Capture Output Filename"; my $wildcard = "*.bmp"; my $defaultDir = "."; my $defaultFilename = "capture.bmp"; my $filevalue = $defaultFilename; my $fileDialog = Wx::FileDialog->new($self, $caption, $defaultDir, + $defaultFilename, $wildcard, wxFD_SAVE | wxFD_OVERWRITE_P +ROMPT); if ($fileDialog->ShowModal() == wxID_OK ) { $filevalue = $fileDialog->GetPath(); } $fileDialog->Destroy; return $filevalue; } 1; __END__ =head1 NAME WxScreenCapture.pm =head1 SYNOPSIS Module to capture a screen to a file. Written in wxPerl. =head1 DESCRIPTION This module exports three subroutines: take_screenshot, file_entry_dia +log_simple, and file_entry_dialog_standard. take_screenshot uses Wx:: +ScreenDC and Wx::MemoryDC device contexts and a Wx::Bitmap. Blit copi +es the ScreenDC to the MemoryDC Bitmap which is then copied to an out +put file. file_entry_dialog_simple provides a text_entry_dialog for r +equesting the capture filename. file_entry_dialog_standard provides t +he full directory/file entry screen for requesting the capture pathna +me. If the frame you wish to capture is smaller than the full screen, the +background windows will also be captured. =head1 USAGE use Wx qw(:everything); use Wx::Event qw( EVT_PAINT ); use WxScreenCapture; my $app = Wx::SimpleApp->new; my $frame = Wx::Frame->new(undef, -1, "Screen Capture Module Example", + [0,0], [1024,768] , wxDEFAULT_FRAME_STYLE); EVT_PAINT( $frame, \&onPaint); my $file = file_entry_dialog_standard($frame); $frame->Show; take_screenshot($frame, $file); $app->MainLoop; sub onPaint{} #paint the screen to be captured here. =head1 AUTHOR James M. Lynes, Jr. <jmlynesjr@gmail.com> Lakeland, Florida USA October 21,2012. Original author: "PodMaster" from Perl Monks-2003 (no idea of real ide +ntity-not active for 6+ years) =head1 BUGS/FEATURES To capture the complete sample graphic(not included in this module def +inition), the frame size had to be set to the screen size of [1024x768]. Using wxDefaultSize ca +used the screen to paint in two passes and the capture to clip. If the frame is not defined large enough, par +ts of underlying windows will also be captured. =head1 SEE ALSO "The wxBook" - Cross-Platform GUI Programming with wxWidgets - Smart, Hock, and Csomor The wxWidgets documentation L<http://www.wxwidgets.org/> The Citrus Perl Distribution L<http://www.citrusperl.com/> =head1 LICENSE This program is free software; you can redistribute it and/or modify i +t under the same terms as Perl itself. =cut
Test Script
use strict; use warnings; use Wx qw(:everything); use Wx::Event qw( EVT_PAINT ); use WxScreenCapture; =head2 # # Main Application # =cut my $app = Wx::SimpleApp->new; my $frame = Wx::Frame->new(undef, -1, "Screen Capture Module Example", + [0,0], [1024,768] , wxDEFAULT_FRAME_STYLE); EVT_PAINT( $frame, \&onPaint); my $file = file_entry_dialog_standard($frame); $frame->Show; take_screenshot($frame, $file); $app->MainLoop; =head2 # # Generate a sample graphic screen to capture # =cut sub onPaint{ my($self,$event)=@_; my $screen = Wx::PaintDC->new($self); $screen->SetBackgroundMode( wxTRANSPARENT ); $screen->SetFont(Wx::Font->new( 12, wxFONTFAMILY_ROMAN, wxNORMAL, +wxBOLD)); for(0..17){ my $c = $_ * 15; $screen->SetTextForeground( Wx::Colour->newRGB(0,0,$c) +); $screen->DrawRotatedText("wxPerl",100 ,100 ,$c); } $screen->DrawRotatedText("wxPerl and PodMaster",000 ,4 +00 ,0); $screen->DrawRotatedText("are messing up your screen", +000 ,450 ,0); for(0..17){ my $c = $_ * 15; $screen->SetTextForeground( Wx::Colour->newRGB(0,$c,0) +); $screen->DrawRotatedText("wxPerl",200 ,200 ,$c); } $screen->DrawRotatedText("wxPerl and PodMaster",100 ,5 +00 ,0); $screen->DrawRotatedText("are messing up your screen", +100 ,550 ,0); for(0..17){ my $c = $_ * 15; $screen->SetTextForeground( Wx::Colour->newRGB($c,0,0) +); $screen->DrawRotatedText("wxPerl",300 ,300 ,$c); } $screen->DrawRotatedText("wxPerl and PodMaster",200 ,6 +00 ,0); $screen->DrawRotatedText("are messing up your screen", +200 ,650 ,0); }

Replies are listed 'Best First'.
Re: Screen Capture Module via wxPerl
by zentara (Archbishop) on Oct 23, 2012 at 18:37 UTC
    It dosn't seem to capture a Screenshot for me, on Linux. The script runs, asks for a filename, and even writes a little demo screen; but the capture.bmp produced is just a thin colored bar.... not the Screen.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      zentara, I don't know what to say. It runs correctly for me. Will have to wait and see if others report similar problems and go from there.

      I am running Ubuntu 10.10, Citrus Perl 16.1, wxWidgets 2,8.x, and Wx 0.9914.

      James

Re: Screen Capture Module via wxPerl
by Anonymous Monk on Oct 24, 2012 at 06:39 UTC

      Great. Thanks. Will do. Proper usage follows learning the basic syntax.:)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1000505]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-19 02:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found