捕获虚拟屏幕(所有显示器)

发布于 2024-10-28 03:10:29 字数 396 浏览 5 评论 0原文

我正在尝试获取整个虚拟屏幕的屏幕截图。这意味着,不仅是主屏幕的图像,还包括连接到计算机的每个屏幕的图像。

有办法做到这一点吗?我尝试使用这个,但它不起作用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轮廓§ 2024-11-04 03:10:29

文档说:图形。 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 execute CopyFromScreen() 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 the Size(1280,1024). For the second one you need to call memoryGraphics.CopyFromScreen(0, 0, *1280*, 0, s); and s would be the Size(1920, 1200).
Hope this helps.

蓝天白云 2024-11-04 03:10:29

就像伊戈尔和汉斯所说,你必须指出源坐标:

Bitmap screenshot = new Bitmap(
    SystemInformation.VirtualScreen.Width, 
    SystemInformation.VirtualScreen.Height, 
    PixelFormat.Format32bppArgb);

Graphics screenGraph = Graphics.FromImage(screenshot);

screenGraph.CopyFromScreen(
    SystemInformation.VirtualScreen.X, 
    SystemInformation.VirtualScreen.Y, 
    0, 
    0, 
    SystemInformation.VirtualScreen.Size, 
    CopyPixelOperation.SourceCopy);

Like Igor and Hans have said, you have to indicate the source coordinate :

Bitmap screenshot = new Bitmap(
    SystemInformation.VirtualScreen.Width, 
    SystemInformation.VirtualScreen.Height, 
    PixelFormat.Format32bppArgb);

Graphics screenGraph = Graphics.FromImage(screenshot);

screenGraph.CopyFromScreen(
    SystemInformation.VirtualScreen.X, 
    SystemInformation.VirtualScreen.Y, 
    0, 
    0, 
    SystemInformation.VirtualScreen.Size, 
    CopyPixelOperation.SourceCopy);
白昼 2024-11-04 03:10:29

Igor 是对的,为 SourceX/Y 参数传递 0, 0 是不正确的。迭代 Screen.AllScreens 属性中的 Screen 实例以查找边界矩形。请注意,CopyFromScreen() 有一个错误,它无法捕获分层窗口(设置了 TransparencyKeyOpacity 的窗口)。检查我的答案 此线程 的解决方法。

请注意,捕获整个桌面并不总是可行的,当屏幕未排列成完美的矩形时,您会看到很多黑色,并且 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 that CopyFromScreen() has a bug, it cannot capture layered windows (the kind that has TransparencyKey or Opacity 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文