Problem using .Save for image RE>Object reference not set to an instance of an object

Nic

New member
Joined
Mar 30, 2005
Messages
1
Programming Experience
1-3
Problem using .Save for image RE>Object reference not set to an instance of an object

Hello,
I am experiencing a weird bug where the following code snippet (not taking credit for the code) works properly the first 8-10 times and then throws an Unhandled Exception.

I have isolated the problem to when the code tries to run the .Save command on an image type. I don't quite understand why and would appreciate any feedback.

(I need to be able to do screen captures anywhere between 100-4000 times over the space of 1-2 hours of my application running;
using different filenames (increments) still causes the error)

I am currently using VB.NET from VS.NET 2002; .NET Framework v1.0.3705

VB.NET:
 Public Function GetScreenCapture( _ 
 
Optional ByVal FullScreen As Boolean = False) As Image
' Captures the current screen and returns as an Image object
Dim objSK As SendKeys
Dim imgCapture As Image
If FullScreen = True Then
' Print Screen pressed twice here as some systems
' grab active window "accidentally" on first run
objSK.SendWait("{PRTSC 2}")
 
Else
objSK.SendWait("%{PRTSC}")
End If
Dim objData As IDataObject = Clipboard.GetDataObject()
Return objData.GetData(DataFormats.Bitmap)
End Function
 
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
GetScreenCapture(True).Save("c:\screengrab.bmp")
End Sub

Any suggestions would be appreciated.
Sincerely
 
Top Bottom