即使Clipboard.ContainsImage = True,Picturebox的图像也没什么?

时间:2015-02-19 12:41:11

标签: vb.net winforms graphics picturebox printscreen

我正在创建一个不断发送密钥“{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

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

1 个答案:

答案 0 :(得分:0)

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

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

  3. 将图像分配给picturebox图像而不是backgroundimage是很正常的。如果应用程序中的某些内容正在为picturebox1.image或blanking picturebox1.image分配位图,它将覆盖picturebox1.backgroundimage中的屏幕截图。