、ジャストウィンドウの位置とサイズを確認し、この長方形にコピーを縮小:アクティブな現在のコピーのために
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
https://stackoverflow.com/a/1163770/1271037 – dovid
lomed!どのようにc# – user206402