VB.NET中的屏幕截图程序

编程入门 行业动态 更新时间:2024-10-28 05:19:54
本文介绍了VB.NET中的屏幕截图程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个应用程序来捕获桌面屏幕截图。它与我在表单中使用的按钮非常好。但是现在我想让这个东西自动使用计时器,但是每当我尝试运行程序 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=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....

解决方案

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

更多推荐

VB.NET中的屏幕截图程序

本文发布于:2023-11-03 12:01:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:截图   屏幕   程序   VB   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!