我的目标是能够在另一个应用程序处于全屏模式时捕获屏幕的整个区域。例如,我跑了osu!在全屏幕中,使用了我的屏幕捕获应用程序(带有键盘快捷键),它显示为空白。我也用CSGO对此进行了测试。
我做了很多研究,但找不到我特别想要的东西。
我尝试的第一件事是(我自己的想法):
Dim area As Rectangle = Screen.GetBounds(New Point(0, 0)) ' get screen size
Dim bmp As Bitmap = New Bitmap(area.Width, area.Height, Imaging.PixelFormat.Format24bppRgb) ' setup new image from the screen
Dim graphic As Graphics = Graphics.FromImage(bmp) ' store in a graphic
graphic.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy) ' convert to something useful
Return bmp
然而,这是返回空白图像的代码。它在拍摄窗口应用程序时非常有效。
然后我看了一篇关于SO的文章(在底部链接)并且正在使用它。注意,我自己将它转换为VB.NET。
<DllImport("User32.dll", SetLastError:=True)> _
Private Shared Function PrintWindow(ByVal hwnd As IntPtr, ByVal hdc As IntPtr, ByVal nFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowRect(ByVal handle As IntPtr, ByRef rect As Rectangle) As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="GetDesktopWindow")> _
Private Shared Function GetDesktopWindow() As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function
Dim intptrDesktop As IntPtr = FindWindowEx(GetDesktopWindow(), IntPtr.Zero, "Progman", "Program Manager")
Dim area As New Rectangle()
GetWindowRect(intptrDesktop, area)
Dim bmp As Bitmap = New Bitmap(area.Width, area.Height, Imaging.PixelFormat.Format24bppRgb) ' to avoid color issues
Dim graMem As Graphics = Graphics.FromImage(bmp)
Dim dc As IntPtr = graMem.GetHdc()
PrintWindow(intptrDesktop, dc, 0)
graMem.ReleaseHdc(dc)
Return bmp
这仅适用于捕获桌面,但我想知道是否可以通过抓取它的HWND来将其用于全屏捕获?
我试图找到最简单的方法。我应该怎么做呢?
参考