使用 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
以下代码是我执行上述功能的方式,如果它有任何区别,我不知道我认为确实如此:
SaveScreen("C:\Lexer_trace\screen.png")
现在,我需要能够拍摄整个屏幕的照片,而不仅仅是聚焦的窗口。我该怎么做?
预先感谢,
洛根
Currently I have the following VB.NET code to make a screenshot of my desktop, but it only takes a picture of the active screen:
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
The following code is how I execute the above function, if it makes any difference, which I don't think it does:
SaveScreen("C:\Lexer_trace\screen.png")
Now, I need to be able to take a picture of the entire screen, not just the focused window. How would I do this?
Thanks in advance,
Logan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用 System.Drawing.Graphics.CopyFromScreen()
参见此处从屏幕复制
只需查询屏幕的完整尺寸作为点传递。
类似于
.CopyFromScreen()
的内容You should be using
System.Drawing.Graphics.CopyFromScreen()
See Here to copy from a screen
Simply query the full size of the screen to pass in as points.
Something similar to what you have with
.CopyFromScreen()
您的评论说您正在发送
alt + printscreen
,它只捕获当前活动的窗口。如果您只发送
printscreen
,它应该捕获整个桌面。Your comment says you are sending
alt + printscreen
which does just capture the currently active window.If you just send
printscreen
it should capture the whole desktop.好吧,直接的解决办法是只发送打印屏幕:
但这充其量只是一个蹩脚的黑客攻击。要可靠地截取屏幕截图,您需要使用 P/Invoke 来
GetDC
桌面句柄 (0) 并将BitBlt
将其内容转换为Bitmap< /代码>。完成之前,请不要忘记
ReleaseDC
桌面的 DC。或者使用
Graphics.CopyFromScreen
< /a>Well the immediate fix would be to send only print screen:
But that's a lame hack at best. To take a screenshot of the screen reliably, you need to use P/Invoke to
GetDC
of the desktop handle (0) andBitBlt
its contents into aBitmap
. Don't forget toReleaseDC
the desktop's DC before you're done.Or use
Graphics.CopyFromScreen
您是否也尝试过不发送 Alt 键?
像这样的东西:
Have you tried without sending the Alt key too?
Something like: