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



我试图捕捉计算机上的任何和所有屏幕,我试图摆弄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

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

你很接近。我做了一些调整,可以确认这对我来说是有效的。

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

最新更新