简体   繁体   English

如何选择ImageGrab.grab()在多显示器设置中抓取哪个屏幕?

[英]How to select which screen ImageGrab.grab() grabs in a multi-monitor setup?

就像标题所说,我很好奇是否有办法配置ImageGrab.grab()模块来抓取,例如,在多显示器设置中,而不是左边的屏幕。

Unfortunately it isn't possible, due to the manner in which the PIL obtains the dimensions of the display device. 遗憾的是,由于PIL获得显示装置的尺寸的方式,这是不可能的。 When it obtains the Device Context, it does obtain one for all attached monitors: 获取设备上下文时,它为所有连接的监视器获取一个:

screen = CreateDC("DISPLAY", NULL, NULL, NULL); 

( display.c , line 296, version 1.1.7) display.c ,第296行,1.1.7版)

However, to get the display dimensions, it uses this code: 但是,要获取显示尺寸,它使用以下代码:

width = GetDeviceCaps(screen, HORZRES);
height = GetDeviceCaps(screen, VERTRES);

( display.c , lines 299-300, version 1.1.7) display.c ,第299-300行,1.1.7版)

Which only returns the dimensions of the primary, active monitor. 仅返回主要活动监视器的尺寸。 All subsequent operations are done with these width and height values, resulting in a final image that is only the size of the primary display. 所有后续操作都使用这些宽度和高度值完成,从而产生的最终图像仅为主显示的大小。


In order to receive a screengrab of all attached monitors, those two lines would need to be replaced with something like: 为了接收所有连接的监视器的屏幕抓取,这两行将需要替换为:

width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
height = GetSystemMetrics(SM_CYVIRTUALSCREEN);

After which you'd need to recompile PIL. 之后你需要重新编译PIL。 This would provide you with the dimensions of the virtual screen, which is "... the bounding rectangle of all display monitors." 这将为您提供虚拟屏幕的尺寸,即“......所有显示器的边界矩形”。 [ MSDN ] [ MSDN ]

A more correct implementation would be using EnumDisplayMonitors to obtain device contexts for the individual monitors, along with altering ImageGrab.grab()'s interface (or adding a new function) to allow for the selection of a specific monitor, of whose device context would be used for the remaining operations. 更正确的实现是使用EnumDisplayMonitors获取各个监视器的设备上下文,同时更改ImageGrab.grab()的接口(或添加新功能)以允许选择特定的监视器,其设备上下文将是用于剩余的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM