我创建了一个应用程序来捕获桌面屏幕截图。它与我在表单中使用的按钮非常好。但是现在我想让这个东西自动使用计时器,但是每当我尝试运行程序 NullReferenceException 发生时,任何人都可以告诉我这里发生了什么。
TimerCapture interval = 5 TimerSave interval = 6这是代码可以告诉你的情况:
公共类Form1 Private Sub timerCapture_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs)处理timerCapture.Tick Dim bounds As Rectangle Dim screenshot As System .Drawing.Bitmap Dim图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 =截图 End Sub 私有子计时器Save_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs)句柄timerSave.Tick Me.PictureBox1.Image.Save(d:\\\capture.bmp) End Sub Private Sub Form1_Load(ByVal发件人As System.Object,ByVal e As System.EventArgs)处理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)处理timerClose.Tick Me.Close() End Sub Private Sub capture_btn_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理capture_btn.Click Dim bounds As Rectangle Dim screenshot As System.Drawing.Bitmap Dim graph As Graphics bounds = Scre en.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 私人Sub save_btn_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理save_btn.Click Me.PictureBox1.Image.Save(d:\\\capture.bmp)结束Sub 结束类提前感谢...
$ b $我认为问题在timerSave_Tick中,如果由于某种原因你还没有在TimerCapture_Tick中重视Me.PictureBox1.Image,那么它会抛出NullReferenceException而尝试访问PictureBox1.Image。尝试以这种方式修改:
P如果(Me.PictureBox1.Image IsNot Nothing)然后 Me.PictureBox1.Image.Save( d:\\\capcap.bmp)如果结束Sub无论如何,您应该可以在Visual Studio下进行调试,以查看Exception被抛出的位置。
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=6Here 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 ClassThanks in advance....
解决方案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 SubAnyway, you should be able to debug under Visual Studio, to see where the Exception is thrown..
更多推荐
VB.NET中的屏幕截图程序
发布评论