我正在使用VB.NET尝试捕获屏幕的一部分,所以我在几个地方找到了这个代码来捕获整个屏幕:
Dim screenSize = SystemInformation.PrimaryMonitorSize
Dim bitmap = New Bitmap(screenSize.Width, screenSize.Height)
Using g As Graphics = Graphics.FromImage(bitmap)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
End Using
bitmap.Save("c:\scratch\screenshot.png", System.Drawing.Imaging.ImageFormat.Png)
这没有错误抛出,文件创建成功,但生成的图像完全透明(大小正确) - 我缺少什么?
This question涉及相同的错误,但解决方案是使用Win32 GDI,我想尽可能避免
答案 0 :(得分:0)
由于CopyFromScreen方法存在问题,我最终决定使用打印屏幕选项:
'declarations
Private Const VK_SNAPSHOT As Integer = &H2C
<DllImport("user32.dll")>
Public Function keybd_event(ByVal key As Integer, ByVal dummy As Integer, ByVal flags As Integer, ByVal info As IntPtr) As IntPtr
End Function
'Usage
Dim loopCount As Integer = 0
Clipboard.Clear()
keybd_event(VK_SNAPSHOT, 0, 0, IntPtr.Zero)
Application.DoEvents()
Do Until Clipboard.ContainsImage Or (loopCount > 10)
loopCount += 1
System.Threading.Thread.Sleep(100)
Application.DoEvents()
Loop
If Clipboard.ContainsImage Then
Return Clipboard.GetImage
Else
Return Nothing
End If