0

我正在使用以下代码仅捕获指定的应用程序区域:

    '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 像素高

但是无论我把这些点放在哪里,我都无法让它与上面的当前代码一起工作。

4

1 回答 1

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)
于 2013-09-09T14:46:15.027 回答