繁体   English   中英

VB.NET中的屏幕捕获程序

[英]Screen Capture program in VB.NET

我创建了一个捕获桌面屏幕截图的应用程序。 与我在表单中使用的按钮配合使用时效果很好。 但是,现在我想使用计时器使它自动运行,但是每当我尝试运行程序NullReferenceException时,任何人都可以告诉我这里出了什么问题。

TimerCapture interval=5

TimerSave interval=6

以下代码可以告诉您这种情况:

Public Class Form1


    Private Sub timerCapture_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerCapture.Tick
        Dim bounds As 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.Format32bppArgb)
        graph = Graphics.FromImage(screenshot)
        graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
        PictureBox1.Image = screenshot
    End Sub




    Private Sub timerSave_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerSave.Tick
        Me.PictureBox1.Image.Save("d:\\capture.bmp")

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Me.WindowState = FormWindowState.Minimized

        'Me.ShowInTaskbar = False

    End Sub

    Private Sub timerClose_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerClose.Tick
        Me.Close()

    End Sub

    Private Sub capture_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles capture_btn.Click
        Dim bounds As 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.Format32bppArgb)
        graph = Graphics.FromImage(screenshot)
        graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
        PictureBox1.Image = screenshot
    End Sub

    Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
        Me.PictureBox1.Image.Save("d:\\capture.bmp")
    End Sub
End Class

提前致谢....

我认为问题出在timerSave_Tick中,如果由于某种原因您尚未对timerCapture_Tick中的Me.PictureBox1.Image进行赋值,则在尝试访问PictureBox1.Image时会抛出NullReferenceException。

尝试以这种方式修改它:

Private Sub timerSave_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerSave.Tick
    If(Me.PictureBox1.Image IsNot Nothing) Then
        Me.PictureBox1.Image.Save("d:\\capture.bmp")
    End If
End Sub

无论如何,您应该能够在Visual Studio下进行调试,以查看在何处引发了异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM