1 package Imager::Screenshot;
3 use vars qw(@ISA $VERSION @EXPORT_OK);
8 @EXPORT_OK = 'screenshot';
15 # try XSLoader first, DynaLoader has annoying baggage
17 XSLoader::load('Imager::Screenshot' => $VERSION);
21 push @ISA, 'DynaLoader';
22 bootstrap Imager::Screenshot $VERSION;
27 # lose the class if called as a method
28 @_ % 2 == 1 and shift;
41 if (defined $opts{hwnd}) {
43 or die "Win32 driver not enabled\n";
44 $result = _win32($opts{hwnd}, $opts{decor}, $opts{left}, $opts{top},
45 $opts{right}, $opts{bottom});
47 elsif (defined $opts{id}) { # X11 window id
49 or die "X11 driver not enabled\n";
50 $result = _x11($opts{display}, $opts{id}, $opts{left}, $opts{top},
51 $opts{right}, $opts{bottom});
53 elsif ($opts{widget}) {
55 my $top = $opts{widget}->toplevel;
56 my $sys = $top->windowingsystem;
57 if ($sys eq 'win32') {
58 unless (defined &_win32) {
59 Imager->_set_error("Win32 Tk and Win32 support not built");
62 $result = _win32(hex($opts{widget}->id), $opts{decor},
63 $opts{left}, $opts{top}, $opts{right}, $opts{bottom});
65 elsif ($sys eq 'x11') {
66 unless (defined &_x11) {
67 Imager->_set_error("X11 Tk and X11 support not built");
71 my $id_hex = $opts{widget}->id;
72 $opts{widget}->can('frame')
73 and $id_hex = $opts{widget}->frame;
75 # is there a way to get the display pointer from Tk?
76 $result = _x11($opts{display}, hex($id_hex), $opts{left}, $opts{top},
77 $opts{right}, $opts{bottom});
80 Imager->_set_error("Unsupported windowing system '$sys'");
86 defined &_win32 ? _win32(0, $opts{decor}, $opts{left}, $opts{top},
87 $opts{right}, $opts{bottom}) :
88 defined &_x11 ? _x11($opts{display}, 0, $opts{left}, $opts{top},
89 $opts{right}, $opts{bottom}) :
90 die "No drivers enabled\n";
94 Imager->_set_error(Imager->_error_as_msg());
98 # RT #24992 - the Imager typemap entry is broken pre-0.56, so
100 return bless { IMG => $result }, "Imager";
112 my $display = _x11_open(@_);
114 Imager->_set_error(Imager->_error_as_msg);
131 Imager::Screenshot - screenshot to an Imager image
135 use Imager::Screenshot 'screenshot';
138 my $img = screenshot();
141 my $img2 = screenshot(hwnd => $hwnd);
144 my $img3 = screenshot(display => $display, id => $window_id);
147 my $display = Imager::Screenshot::x11_open();
148 Imager::Screenshot::x11_close($display);
150 # test for win32 support
151 if (Imager::Screenshot->have_win32) { ... }
153 # test for x11 support
154 if (Imager::Screenshot->have_x11) { ... }
159 Imager::Screenshot captures either a desktop or a specified window and
160 returns the result as an Imager image.
162 Currently the image is always returned as a 24-bit image.
166 =item screenshot hwnd => I<window handle>
168 =item screenshot hwnd => I<window handle>, decor => <capture decorations>
170 Retrieve a screenshot under Win32, if I<window handle> is zero,
173 By default, window decorations are not captured, if the C<decor>
174 parameter is set to true then window decorations are included.
176 =item screenshot id => I<window id>
178 =item screenshot id => I<window id>, display => I<display object>
180 Retrieve a screenshot under X11, if I<id> is zero, capture the root
181 window. I<display object> is a integer version of an X11 C< Display *
182 >, if this isn't supplied C<screenshot()> will attempt connect to the
183 the display specified by $ENV{DISPLAY}.
185 Note: taking a screenshot of a remote display is slow.
189 If no C<id> or C<hwnd> parameter is supplied:
195 if Win32 support is compiled, return screenshot(hwnd => 0).
199 if X11 support is compiled, return screenshot(id => 0).
207 You can also supply the following parameters to retrieve a subset of
230 If left or top is negative, then treat that as from the right/bottom
233 If right ot bottom is zero or negative then treat as from the
234 right/bottom edge of the window.
236 So setting all 4 values to 0 retrieves the whole window.
238 # a 10-pixel wide right edge of the window
239 my $right_10 = screenshot(left => -10, ...);
241 # the top-left 100x100 portion of the window
242 my $topleft_100 = screenshot(right => 100, bottom => 100, ...);
244 # 10x10 pixel at the bottom right corner
245 my $bott_right_10 = screenshot(left => -10, top => -10, ...);
249 Returns true if Win32 support is available.
253 Returns true if X11 support is available.
255 =item Imager::Screenshot::x11_open
257 =item Imager::Screenshot::x11_open I<display name>
259 Attempts to open a connection to either the display name in
260 $ENV{DISPLAY} or the supplied display name. Returns a value suitable
261 for the I<display> parameter of screenshot, or undef.
263 =item Imager::Screenshot::x11_close I<display>
265 Closes a display returned by Imager::Screenshot::x11_open().
271 screenshot() sets a number of tags in the images it returns, these are:
277 ss_left - the distance between the left side of the window and the
278 left side of the captured area. The same value as the I<left>
279 parameter when that is positive.
283 ss_top - the distance between the top side of the window the top side
284 of the captured area. The same value at the I<top> parameter when
289 ss_window_width - the full width of the window.
293 ss_window_height - the full height of the window.
297 ss_type - the type of capture done, either "Win32" or "X11".
301 To cheaply get the window size you can capture a single pixel:
303 my $im = screenshot(right => 1, bottom => 1);
304 my $window_width = $im->tags(name => 'ss_window_width');
305 my $window_height = $im->tags(name => 'ss_window_height');
309 It's possible to have more than one grab driver available, for
310 example, Win32 and X11, and which is used can have an effect on the
313 Under Win32, if there's a screesaver running, then you grab the
314 results of the screensaver.
316 Grabbing the root window on a rootless server (eg. Cygwin/X) may not
317 grab the background that you see. In fact, when I tested under
318 Cygwin/X I got the xterm window contents even when the Windows
319 screensaver was running. The root window captured appeared to be that
320 generated by my window manager.
324 Imager::Screenshot is licensed under the same terms as Perl itself.
328 Tony Cook <tonyc@cpan.org>