我使用以下代码仅捕获指定的应用区域:
'find running instance of calculator
Dim p As Process = Process.GetProcessesByName("calc").FirstOrDefault
If p IsNot Nothing Then
'bring window to front
AppActivate(p.Id)
'get location + size of window
Dim r As New RECT
GetWindowRect(p.MainWindowHandle, r)
'create new bitmap + copy calc window
Dim img As New Bitmap(r.right - r.left, r.bottom - r.top)
Dim gr As Graphics = Graphics.FromImage(img)
gr.CopyFromScreen(New Point(r.left, r.top), Point.Empty, img.Size)
'save image + launch in default viewer
img.Save("test.png", Drawing.Imaging.ImageFormat.Png)
Process.Start("test.png")
End If
它可以很好地捕获正确的应用程序,但我只想捕获该应用程序中的#2按钮,而不是完整的应用程序屏幕。
我知道在应用程序中按钮位于: 左起97像素 顶部189个像素
#2按钮本身的大小是: 36像素宽 29像素高度
但无论我把这些点放在哪里,我都无法使用上面的当前代码。
答案 0 :(得分:2)
我无法捕获#2按钮,但很可能是因为其他版本或其他程序,如果你没有使用Windows计算器,但这应该让你走上正确的轨道:
尝试更换:
Dim img As New Bitmap(r.right - r.left, r.bottom - r.top)
Dim gr As Graphics = Graphics.FromImage(img)
gr.CopyFromScreen(New Point(r.left, r.top), Point.Empty, img.Size)
使用:
'set the size of the to be captured area(size of button in this case)
Dim img As New Bitmap(36, 29)
Dim gr As Graphics = Graphics.FromImage(img)
'set offsets and use image size to set region
gr.CopyFromScreen(New Point(r.Left + 97, r.Top + 189), Point.Empty, img.Size)