【问题标题】:PrintWindow / CopyFromScreen getting selected areaPrintWindow / CopyFromScreen 获取选定区域
【发布时间】:2013-09-10 02:26:27
【问题描述】:

大家好,我这里有这段代码:

Dim p As Process = Process.GetProcessesByName("Cal").FirstOrDefault
Dim target_hwnd As Long = FindWindow(vbNullString, "Calculator")

If p IsNot Nothing Then
    SetWindowPos(target_hwnd, 0, winSize(0), winSize(1), winSize(2), winSize(3), 0)
    AppActivate(p.Id)

    Dim img As New Bitmap(145, 145) 'size fo the caption area
    Dim gr As Graphics = Graphics.FromImage(img)

    'sets the offsets and use image size to set region
    gr.CopyFromScreen(New Point(winSize(0) + 44, winSize(1) + 179), Point.Empty, img.Size)
    img.Save("test.jpg", Drawing.Imaging.ImageFormat.Jpeg)
    Process.Start("test.jpg")
End If

只要我有窗口在视图中,它就可以很好地拍摄屏幕截图,没有任何问题。但是,当我将表单移出屏幕(我看不到它)时,它只会捕获一个黑色图像

我一直在尝试这段代码:

Private Declare Function PrintWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As UInt32) As Boolean
Dim screenCapture As Bitmap
Dim otherForm As New Form

Private Sub CaptureScreen()
    Dim target_hwnd As Long = FindWindow(vbNullString, "Calculator")
    SetWindowPos(target_hwnd, 0, winSize(0), winSize(1), winSize(2), winSize(3), 0)

    screenCapture = New Bitmap(245, 245)
    Dim g As Graphics = Graphics.FromImage(screenCapture)
    Dim hdc As IntPtr = g.GetHdc
    Form1.PrintWindow(target_hwnd, hdc, Nothing)
    g.ReleaseHdc(hdc)
    g.Flush()
    g.Dispose()

    If IO.File.Exists("d:\test.jpg") Then
        IO.File.Delete("d:\test.jpg")
    End If

    screenCapture.Save("d:\test.jpg", Drawing.Imaging.ImageFormat.Jpeg)
End Sub

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    CaptureScreen()
End Sub

现在,即使窗口不在屏幕上,上面的代码确实也会捕获图像。上面代码的问题是我不能告诉它只捕获那个窗口中的一个区域,而我可以用我第一次发布的 CopyFromScreen 来完成。

这可以使用 PrintWindow 吗?

【问题讨论】:

  • 自己裁剪图像。
  • @HansPassant 感谢您指出正确的方向。

标签: vb.net graphics screenshot screen-capture


【解决方案1】:

我能够做到这一点:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim fileName = "Calculator.jpg"
    Dim fileNameCrop = "Calculator-crop.jpg"
    '     |--b|---|x|
    '     |   |     |            a|-Form Left to image area
    '     |   V     |             |  b|-Form Top to image area
    '     a-->[c]   |             |   |   c|-Image area Width to capture
    '     |         |             |   |    |  c|-Image area Height to capture
    '     |_________|             V   V    V   V     
    Dim CropRect As New Rectangle(97, 189, 36, 29)
    Dim OrignalImage = Image.FromFile(fileName)
    Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)

    Using grp = Graphics.FromImage(CropImage)
        grp.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
        grp.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        grp.DrawImage(OrignalImage, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
        CropImage.Save(fileNameCrop)
    End Using

    OrignalImage.Dispose()
    CropImage.Dispose()
    'delete org image
    If FileIO.FileSystem.FileExists(fileName) Then FileIO.FileSystem.DeleteFile(fileName)
End Sub

我从 OP 中发布的第一个代码中保存表单图像后,只需裁剪掉该区域。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多