1

我正在制作一个不断发送密钥“ {PRTSC}”然后设置的程序PictureBox1.BackgroundImage = My.Computer.Clipboard.GetImage

起初它工作正常,但在一两分钟后,图片框变为空白并且没有给出错误。

我的代码是:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If Not My.Computer.Clipboard.ContainsImage Then
        SendKeys.Send("{PRTSC}")
    Else
        PictureBox1.BackgroundImage = My.Computer.Clipboard.GetImage
        My.Computer.Clipboard.Clear()
    End If
End Sub

我努力了:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    'SendKeys.Send("{PRTSC}")
    'If My.Computer.Clipboard.ContainsImage Then PictureBox1.BackgroundImage = My.Computer.Clipboard.GetImage

    Dim bounds As New Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    bounds = Screen.PrimaryScreen.Bounds
    screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height) ', System.Drawing.Imaging.PixelFormat.Format32bppRgb)
    graph = Graphics.FromImage(screenshot)
    graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
    PictureBox1.BackgroundImage = screenshot
    graph.Dispose()
    'screenshot.Save("d:\\dcap.jpg", Imaging.ImageFormat.Bmp)

End Sub

但是尝试处理屏幕截图会立即产生错误。我不知道为什么。

4

1 回答 1

0
  1. {PRTSC} 在有焦点时抓取活动窗口,否则抓取屏幕。

  2. 最好在滴答事件开始时禁用计时器,并在结束时启动它。这可以防止重新进入,并且根据计时器的类型(有计时器控件、system.timers.timer 和 system.threading.timer,每个都有一点不同),您可能需要重新启动计时器每个滴答事件。

  3. 将图像分配给图片框图像而不是背景图像是正常的。如果应用程序中的某些内容将位图分配给picturebox1.image 或空白picturebox1.image,它将覆盖picturebox1.backgroundimage 中的屏幕截图。

于 2015-02-19T17:46:46.790 回答