捕获虚拟屏幕(所有显示器)
我正在尝试获取整个虚拟屏幕的屏幕截图。这意味着,不仅是主屏幕的图像,还包括连接到计算机的每个屏幕的图像。
有办法做到这一点吗?我尝试使用这个,但它不起作用:
Bitmap b = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(b);
this.Size = new Size(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
g.CopyFromScreen(0, 0, 0, 0, b.Size);
I am trying to get a screenshot of the whole virtual screen. This means, an image of not just the primary screen, but every screen connected to the computer.
Is there a way to do that? I tried using this, but it didn't work:
Bitmap b = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(b);
this.Size = new Size(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
g.CopyFromScreen(0, 0, 0, 0, b.Size);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文档说:
图形。 CopyFromScreen(Int32, Int32, Int32, Int32, Size)
:执行颜色数据的位块传输,对应于像素的矩形,从屏幕到绘图表面但虚拟屏幕不一定是矩形:想象两个分辨率分别为 1920x1200 和 1280x1024 的显示器。因此您需要做的是像您一样创建一个位图,然后枚举您的显示器并执行 CopyFromScreen()< 为编辑: 例如,如果您有两台显示器,分辨率为 1280x1024 的显示器位于 1920x1200 显示器的左侧,则坐标 前者的值为 (-1280,0) - (0, 1024),因此您需要执行 memoryGraphics.CopyFromScreen(-1280, 0, 0, 0, s); ,其中 s 是
Size(1280,1024)
对于第二个,您需要调用memoryGraphics.CopyFromScreen(0, 0, *1280*, 0, s); , s 将为
尺寸(1920, 1200)
。希望这有帮助。
The documentation says:
Graphics.CopyFromScreen(Int32, Int32, Int32, Int32, Size)
: Performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics." But the virtual screen is not necessarily a rectangle: imagine two monitors with 1920x1200 and 1280x1024 resolutions. So what you need to do is create a bitmap like you do, then enumerate your monitors and executeCopyFromScreen()
for each of them.Edit: If, for instance, you have two monitors, the one having 1280x1024 resolution standing on the left of 1920x1200 one, then the coordinates of the former would be (-1280,0) - (0, 1024). Therefore you need to execute
memoryGraphics.CopyFromScreen(-1280, 0, 0, 0, s);
where s is theSize(1280,1024)
. For the second one you need to callmemoryGraphics.CopyFromScreen(0, 0, *1280*, 0, s);
and s would be theSize(1920, 1200)
.Hope this helps.
就像伊戈尔和汉斯所说,你必须指出源坐标:
Like Igor and Hans have said, you have to indicate the source coordinate :
Igor 是对的,为 SourceX/Y 参数传递 0, 0 是不正确的。迭代 Screen.AllScreens 属性中的 Screen 实例以查找边界矩形。请注意,
CopyFromScreen()
有一个错误,它无法捕获分层窗口(设置了TransparencyKey
或Opacity
的窗口)。检查我的答案 此线程 的解决方法。请注意,捕获整个桌面并不总是可行的,当屏幕未排列成完美的矩形时,您会看到很多黑色,并且
OutOfMemory
异常在 32 位计算机上并不罕见。高分辨率显示器。Igor is right, passing 0, 0 for the SourceX/Y arguments isn't correct. Iterate the Screen instances in the
Screen.AllScreens
property to find the bounding rectangle. Beware thatCopyFromScreen()
has a bug, it cannot capture layered windows (the kind that hasTransparencyKey
orOpacity
set). Check my answer in this thread for a workaround.Beware that capturing the entire desktop isn't always practical, you'll get lots of black when the screens are not arranged in a perfect rectangle and an
OutOfMemory
exception is not uncommon on a 32-bit machine with high resolution displays.