Screen Capture program in VB.NET

前提是你 提交于 2019-12-20 05:14:36

问题


I had created an application that captures the screenshot of desktop screen. It works very well with button I have used in form. But now I want to make that thing works automatically using timers but whenever I try to run program NullReferenceException occur can any one tell me whats going wrong here.

TimerCapture interval=5

TimerSave interval=6

Here is the code can tell you the scenario:

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

Thanks in advance....


回答1:


I think the problem is in timerSave_Tick, if, for some reason you haven't already valued Me.PictureBox1.Image in timerCapture_Tick, it would throw NullReferenceException while trying to access to PictureBox1.Image.

Try to modify it in such way:

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

Anyway, you should be able to debug under Visual Studio, to see where the Exception is thrown..



来源:https://stackoverflow.com/questions/16460330/screen-capture-program-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!