I have an app which properly captures the image of an application window if it is in the upper-left corner of the primary screen.
But if it is not, the image size is not correct (the window image height becomes stretched if it is against the right margin and down from the screen top. Application at 0,0
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic.Strings
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Public Declare Function GetWindowRect Lib "user32" (ByVal HWND As Integer, ByRef lpRect As Rectangle) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub BtnCapture_Click(sender As Object, e As EventArgs) Handles BtnCapture.Click
Dim FoundApplication As Boolean = False
Dim localAll As Process() = Process.GetProcesses()
Dim rect As New Rectangle
Dim Top As Int32 = 0
Dim Left As Int32 = 0
Dim width As Int32
Dim height As Int32
Dim hwnd As IntPtr
Dim memoryImage As Bitmap
For Each x As Process In localAll
GetWindowRect(x.MainWindowHandle, rect)
If x.ProcessName.ToString = "calc" Then
width = rect.Width
height = rect.Height
Top = rect.Top
Left = rect.Left
hwnd = x.MainWindowHandle
FoundApplication = True
Exit For
End If
Next
If FoundApplication Then
' do nothing - set above
Else
' set the default to entire Primary screen if Calc not found
width = Screen.PrimaryScreen.Bounds.Width
height = Screen.PrimaryScreen.Bounds.Height
End If
Dim MyGraphics As Graphics = Graphics.FromHwnd(hwnd)
Dim s As New Size(width, height)
memoryImage = New Bitmap(width, height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(Top, Left, 0, 0, s)
Clipboard.SetImage(memoryImage)
RtbLog.AppendText(Today().ToShortDateString & " " & Now().ToShortTimeString & vbCrLf)
RtbLog.Paste()
myGraphics.Dispose()
End Sub
End Class
This simple version exhibits the behavior I am dealing with.
If the "calc" is in the upper left corner it's perfect - move it down or to the left and the image includes other parts of the screen and may cut off the image of "calc".