문제

I am using the following code to capture only the app area specified:

    '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

It does just fine in capturing the correct app but I am trying to just capture the #2 button within that app only and not the full app screen.

I know that within the app the button would be located: 97 pixels from the left 189 pixels from the top

And the size of the #2 button itself is: 36 pixels width 29 pixels height

But I have not been able to get that to work with the current code above no matter where I put those points.

도움이 되었습니까?

해결책

I was not able to capture the #2 button but that could very well be because of other versions or other program if you didnt ment the windows calculator, but this should get you on the right track:

try replacing:

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)

with:

'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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top