我正在尝试捕获计算机上的所有屏幕,我尝试摆弄
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
Clipboard.Clear()
SendKeys.SendWait("^{PRTSC}")
While Not Clipboard.ContainsImage()
Thread.Sleep(500)
End While
Dim Screenshot As Image = Clipboard.GetImage()
Return Screenshot