VB.NET - 在计算机上获取所有屏幕的截图

3
我正在尝试在计算机上捕获所有屏幕,我尝试过操作 Screen.AllScreens 和一些关于VirtualScreens的东西,但我不记得了,所以我转向了 PrimaryScreen 确保其他所有内容都能正常工作。
这是我的当前类:
Public Class wmCapture

    Public Shared Function screenCapture()
        Dim userName As String = Environment.UserName
        Dim savePath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        Dim dateString As String = Date.Now.ToString("yyyyMMddHHmmss")
        Dim captureSavePath As String = String.Format("{0}\WM\{1}\capture_{2}.png", savePath, userName, dateString)
        Dim bmp As Bitmap = New Bitmap( _
                            Screen.PrimaryScreen.Bounds.Width, _
                            Screen.PrimaryScreen.Bounds.Height)

        Dim gfx As Graphics = Graphics.FromImage(bmp)

        gfx.CopyFromScreen( _
            Screen.PrimaryScreen.Bounds.Location, _
            New Point(0, 0), Screen.PrimaryScreen.Bounds.Size)

        bmp.Save(captureSavePath)

    End Function

End Class

我应该在屏幕命名空间中使用什么来包含所有活动屏幕?

2个回答

4

你离正确答案很近了。我做了一些调整,确认在我的端上它是有效的。

Public Shared Sub screenCapture()
    Dim userName As String = Environment.UserName
    Dim savePath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    Dim dateString As String = Date.Now.ToString("yyyyMMddHHmmss")
    Dim captureSavePath As String = String.Format("{0}\WM\{1}\capture_{2}.png", savePath, userName, dateString)
    ' This line is modified for multiple screens, also takes into account different screen size (if any)
    Dim bmp As Bitmap = New Bitmap( _
                        Screen.AllScreens.Sum(Function(s As Screen) s.Bounds.Width),
                        Screen.AllScreens.Max(Function(s As Screen) s.Bounds.Height))
    Dim gfx As Graphics = Graphics.FromImage(bmp)
    ' This line is modified to take everything based on the size of the bitmap
    gfx.CopyFromScreen(SystemInformation.VirtualScreen.X,
                       SystemInformation.VirtualScreen.Y,
                       0, 0, SystemInformation.VirtualScreen.Size)
    ' Oh, create the directory if it doesn't exist
    Directory.CreateDirectory(Path.GetDirectoryName(captureSavePath))
    bmp.Save(captureSavePath)
End Sub

非常感谢您,丹。那么我应该使用Sub而不是Function吗?我在我的main.vb(form1)中引用了这个函数和类。 - Sam
我在我的应用程序“加载”事件中有一个不同类别的目录检查,但感谢您添加您的 :) - Sam
除非该方法有返回类型,否则使用“Sub”。在你的情况下,该方法不返回任何东西。 - djv
嗨,丹,再次感谢你的帮助。- 但是正如您所看到的,我的第二个屏幕还是空白的,但是有一个位置可以放置它。- 例如,我在我的第二个监视器上打开了Netflix。- 请注意,我的主要监视器在右侧,第二个在左侧。http://i.imgur.com/LGq6Iuo.png - Sam
1
另一个编辑,处理在奇怪位置(例如您的个人电脑)上的显示。 - djv
显示剩余3条评论

0
Clipboard.Clear()
SendKeys.SendWait("^{PRTSC}")

While Not Clipboard.ContainsImage()
    Thread.Sleep(500)
End While
Dim Screenshot As Image = Clipboard.GetImage()
Return Screenshot

3
欢迎来到Stack Overflow。在编写代码时,附上解释是一个很好的习惯,可以帮助读者理解代码的工作原理,并最终回答问题。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接