Why do I keep getting this error? VB 2010 Express
-
Tuesday, July 17, 2012 2:27 PM
This is what I have, but I get an error when I try debugging this and then it highlights bmp.Save(sPath) in yellow as if it doesn't know what I'm talking about, if someone could come up with a solution to my problem, and maybe some organization tips, that would be great ;)
Public Class Form1
Private Function MakeName(ByVal Pathname As String, ByVal Count As Integer) As StringReturn System.IO.Path.Combine(Pathname, "ss" & Count.ToString & ".bmp")End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickMe.Opacity = 0If My.Computer.FileSystem.FileExists("C:\cache") ThenElseMy.Computer.FileSystem.CreateDirectory("C:\cache")End IfDim SaveCount As Integer = 0Dim sPath As StringDim bmp As BitmapTimer1.Enabled = TrueDosPath = MakeName("C:\cache", SaveCount)SaveCount += 1Loop Until Not System.IO.File.Exists(sPath)bmp = CType(PictureBox1.Image, Bitmap)bmp.Save(sPath)End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.TickDim bounds As RectangleDim screenshot As System.Drawing.BitmapDim graph As Graphicsbounds = Screen.PrimaryScreen.Boundsscreenshot = 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 = screenshotTimer1.Enabled = FalseMe.Opacity = 100End Sub
End ClassI tried to start debugging, I clicked button1 and it highlighted bmp.Save(sPath) and said:
NullReferenceException was unhandled
Object reference not set to an instance of an object.
- Edited by Daniel l3 Tuesday, July 17, 2012 3:11 PM
All Replies
-
Tuesday, July 17, 2012 2:50 PM
"I get an error" What does this mean? What if you continue pressing the shortcut for single-step?
BTW, you should dispose the Graphics object after usage. And I suggest to directly call IO.Directory.Create.
Armin
-
Tuesday, July 17, 2012 3:19 PM
I've changed my post a little, it should be easier to understand what "error" I'm getting."I get an error" What does this mean? What if you continue pressing the shortcut for single-step?
BTW, you should dispose the Graphics object after usage. And I suggest to directly call IO.Directory.Create.
Armin
-
Tuesday, July 17, 2012 3:38 PM
Thx for clarifying.
Probably the timer hasn't ticked yet and there's no image in the picturebox. Is that right? If so, set the button's enabled property to false in the designer and set it to true at run-time in the timer's tick event.
EDIT: I just see "Timer1.Enabled = True" in the button click event. The timer won't tick before the click event handler has exitted. Why don't you start the timer before?
Armin
- Edited by Armin Zingler Tuesday, July 17, 2012 3:40 PM
- Marked As Answer by Daniel l3 Tuesday, July 17, 2012 4:26 PM

