vb.net VB.NET中的截屏程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16460330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:32:46  来源:igfitidea点击:

Screen Capture program in VB.NET

vb.netimageexceptiontimerimage-capture

提问by Vighanesh Gursale

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 NullReferenceExceptionoccur can any one tell me whats going wrong here.

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

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....

提前致谢....

采纳答案by Jamby

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.

我认为问题出在 timerSave_Tick 中,如果由于某种原因您还没有在 timerCapture_Tick 中重视 Me.PictureBox1.Image,那么它会在尝试访问 PictureBox1.Image 时抛出 NullReferenceException。

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..

无论如何,您应该能够在 Visual Studio 下进行调试,以查看抛出异常的位置..