Perl Imager::屏幕截图不使用默认参数进行屏幕截图



我有以下代码:

use Imager::Screenshot 'screenshot';
my $img = screenshot(hwnd => 'active', 
    left => 450, 
    right => 200, 
    top => 50, 
    bottom => 50
);
$img->write(file => 'screenshot.png', type => 'png' ) || 
    print "Failed: ", $img->{ERRSTR} , "n";

它打印:

"

无法在第 3 行调用方法"写入"未定义的值"

但是当我这样做时:

use Imager::Screenshot 'screenshot';
my $img = screenshot(hwnd => 'active', 
    left => 100, 
     right => 300, 
     top => 100, 
     bottom => 300
);
$img->write(file => 'screenshot.png', type => 'png' ) || 
     print "Failed: ", $img->{ERRSTR} , "n";

它确实截取了屏幕截图。为什么左、右、顶和底值在这里很重要?

编辑:经过一番研究,我发现左参数必须小于右参数,顶部参数必须小于底部。

您是否尝试过检查错误?

例如

my $img = screenshot(...) or die Imager->errstr;

编辑:尝试以下代码:

use Imager::Screenshot 'screenshot';
my $img = screenshot(hwnd => 'active',
    left => 450, 
    right => 200, 
    top => 50, 
    bottom => 50
) or die Imager->errstr;
$img->write(file => 'screenshot.png', type => 'png' ) || 
    print "Failed: ", $img->errstr, "n";
我想

这是导致问题的行:

my $img = screenshot(
  hwnd => 'active', 
  left => 450, 
  right => 200, 
  top => 50, 
  bottom => 50
);

看,将leftright参数设置为正值(即> 0),我们设置开始和结束坐标"X-"。但是,对于从窗口最左边缘开始"X"比结束"X"更远是没有意义的。同样的故事也发生在topbottom重视平等。

如果你想要的是"给我一些从左侧 450 像素、右侧 200 像素、顶部和底部边缘 50 像素的窗口",请使用以下命令:

my $img = screenshot(
  hwnd => 'active', 
  left => -200, 
  right => -450, 
  top => -50, 
  bottom => -50
);

最新更新