[英]Taking Photo of Screen with VB.NET
目前我有以下 VB.NET 代碼來截取我的桌面,但它只拍攝活動屏幕的圖片:
Public Function SaveScreen(ByVal theFile As String) As Boolean
Try
SendKeys.Send("%{PRTSC}") '<alt + printscreen>
Application.DoEvents()
Dim data As IDataObject = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
Dim bmp As Bitmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
bmp.Save(theFile, Imaging.ImageFormat.Png)
End If
Clipboard.SetDataObject(0) 'save memory by removing the image from the clipboard
Return True
Catch ex As Exception
Return False
End Try
End Function
以下代碼是我如何執行上面的 function,如果它有任何區別,我認為它不會:
SaveScreen("C:\Lexer_trace\screen.png")
現在,我需要能夠拍攝整個屏幕的照片,而不僅僅是聚焦的 window。 我該怎么做?
提前致謝,
洛根
您應該使用System.Drawing.Graphics.CopyFromScreen()
See Here從屏幕復制
只需查詢屏幕的完整尺寸以作為點傳遞。 與.CopyFromScreen()
類似的東西
Public Sub SaveScreen(filename As String)
Dim screenSize = SystemInformation.PrimaryMonitorSize
Dim bitmap = New Bitmap(screenSize.Width, screenSize.Height)
Dim g = Graphics.FromImage(bitmap)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
g.Flush()
bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png)
End Sub
您的評論說您正在發送alt + printscreen
,它只會捕獲當前活動的 window。
如果您只是發送printscreen
它應該捕獲整個桌面。
那么直接的解決方法是只發送打印屏幕:
SendKeys.Send("{PRTSC}")
但這充其量只是一個蹩腳的黑客攻擊。 要可靠地截取屏幕截圖,您需要使用 P/Invoke 獲取桌面句柄 (0) 的GetDC
並將其內容BitBlt
放入Bitmap
。 完成之前不要忘記ReleaseDC
桌面的 DC。
您是否也嘗試過不發送 Alt 鍵?
就像是:
SendKeys.Send("{PRTSC}") '<printscreen>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.