如何通过vb.net捕获桌面上的活动窗口



我正在使用此代码捕获桌面

Private Function cc() As Bitmap
    Dim s As Screen = Screen.PrimaryScreen
    Dim img As New Bitmap(s.Bounds.Width, s.Bounds.Height)
    Dim gr As Graphics = Graphics.FromImage(img)
    gr.CopyFromScreen(s.Bounds.Location, Point.Empty, s.Bounds.Size)
    Return img
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Me.PictureBox1.Image = cc()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

但是现在我只想捕获活动屏幕,我该怎么做?

问候,,,,

对于从我们的应用程序复制活动窗口,只需检查窗口的位置和大小,然后将复制降低到此矩形:

Public Sub ScreenCopy(Optional file As String = "d:test.jpg")
    Dim bounds = Me.Bounds
    Using bitmap As New Bitmap(bounds.Width, bounds.Height)
        Using g = Graphics.FromImage(bitmap)
            g.CopyFromScreen(New Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size)
        End Using
        bitmap.Save(file, ImageFormat.Jpeg)
        Process.Start(file) ' for test purposes
    End Using
End Sub

对于复制当前活动窗口,即使不是我们应用程序的一部分,您都需要使用API来检测其大小和位置。

为此,使用此类(信用:@kvanttt:https://stackoverflow.com/a/9087955/1271037(

Class ScreenCapturer
    <DllImport("user32.dll")> _
    Private Shared Function GetForegroundWindow() As IntPtr
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef rect As Rect) As IntPtr
    End Function
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure Rect
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure
    Public Function Capture(Optional activeWindowOnly As Boolean = True) As Bitmap
        Dim bounds As Rectangle
        If Not activeWindowOnly Then
            bounds = Screen.GetBounds(Point.Empty)
            CursorPosition = Cursor.Position
        Else
            Dim foregroundWindowsHandle = GetForegroundWindow()
            Dim rect = New Rect()
            GetWindowRect(foregroundWindowsHandle, rect)
            bounds = New Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top)
            CursorPosition = New Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top)
        End If
        Dim result = New Bitmap(bounds.Width, bounds.Height)
        Using g = Graphics.FromImage(result)
            g.CopyFromScreen(New Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size)
        End Using
        Return result
    End Function
    Public Property CursorPosition() As Point
End Class

如何使用:

Public Sub ScreenCopy(Optional file As String = "d:test.jpg")
    Dim sc = New ScreenCapturer()
    Using bitmap = sc.Capture()
        bitmap.Save(file, ImageFormat.Jpeg)
        Process.Start(file) ' for test purposes
    End Using
End Sub

关于dll导入的VB.NET语法的更改:

<DllImport("user32.dll", EntryPoint:="GetForegroundWindow")> Private Shared Function GetForegroundWindow() As IntPtr
 End Function
 <DllImport("user32.dll", EntryPoint:="GetWindowRect")> Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef rect As Rect) As IntPtr
  End Function

最新更新