簡體   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