【问题标题】:Picturebox's image is nothing even though Clipboard.ContainsImage = True?即使 Clipboard.ContainsImage = True,Picturebox 的图像也不算什么?
【发布时间】:2015-04-20 19:36:54
【问题描述】:

我正在制作一个程序,它不断发送密钥“{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

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

【问题讨论】:

    标签: vb.net winforms graphics picturebox printscreen


    【解决方案1】:
    1. {PRTSC} 在获得焦点时抓取活动窗口,否则抓取屏幕。

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

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

    【讨论】:

    • 所以我应该将 Timer1.Enable 设置为 False,然后在代码在 Enable 和 Disable 之间运行后设置为 True?即使我设置图像而不是背景图像,程序将图像设置为 30 秒左右,然后图片框的图像是空白的。
    • tick 设置为 1,在 Form1.Paint 之后开始计时,但是如果都失败了,如何制作一个将在图片框中流式传输的屏幕录像机?
    • 还有你说的定时器有两种,这里应该用什么?
    • 共有三种类型,任何一种都可以正常工作。计时器控件可能很容易使用。
    猜你喜欢
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多