G
Guest
When calling the Save(filename, imageFormat) method of a System.Drawing.Image
object, I get a NullReferenceException shown below. I get the exception if I
create a new stream writer pointed to the file I want to save, and give the
Image.Save method the BaseStream of the new stream writer, or if I use the
overload of Image.Save with the string filename parameter.
The exception does not occur every time, so I suspect something with the
image is causing the issue, however the knownImage below is coming from a
video device and has already been successfully saved to another file.
System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Drawing.SafeNativeMethods.GdipSaveImageToStream(HandleRef
image, IStream stream, Guid& classId, HandleRef encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
at TvTunerVideoGrabber.MainForm.OnFtpTimerTick(Object sender, EventArgs
e) in c:\documents and settings\all users\documents\visual studio
projects\livevideocaptureandrecord\tvtunercapture\mainform.cs:line 559
Sample code:
lock(m_Recorder) //m_Recorder is the object creating
knownImage from the video card. It also does a lock(this) when it
// clones the image from the video device to make knownImage.
{
Image myImage = knownImage.Clone() as Image;
}
sr = new StreamWriter(tempImagePath);
try
{
myImage.Save(sr.BaseStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
finally
{
sr.Close();
}
// the above is wrapped in another try finally that does:
myImage.Dispose();
The other issue is that the above file at tempImagePath is now in use and
cannot be written or deleted, even though I have disposed the streamwriter
and the image objects. However, in disposing the image I get an exception:
System.InvalidOperationException: The object is currently in use elsewhere.
at System.Drawing.Image.Dispose(Boolean disposing)
at System.Drawing.Image.Dispose()
at TvTunerVideoGrabber.MainForm.OnFtpTimerTick(Object sender, EventArgs
e) in c:\documents and settings\all users\documents\visual studio
projects\livevideocaptureandrecord\tvtunercapture\mainform.cs:line 612
Is there something I can look at or test the image before saving it so as
not to leave a file in use?
object, I get a NullReferenceException shown below. I get the exception if I
create a new stream writer pointed to the file I want to save, and give the
Image.Save method the BaseStream of the new stream writer, or if I use the
overload of Image.Save with the string filename parameter.
The exception does not occur every time, so I suspect something with the
image is causing the issue, however the knownImage below is coming from a
video device and has already been successfully saved to another file.
System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Drawing.SafeNativeMethods.GdipSaveImageToStream(HandleRef
image, IStream stream, Guid& classId, HandleRef encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
at TvTunerVideoGrabber.MainForm.OnFtpTimerTick(Object sender, EventArgs
e) in c:\documents and settings\all users\documents\visual studio
projects\livevideocaptureandrecord\tvtunercapture\mainform.cs:line 559
Sample code:
lock(m_Recorder) //m_Recorder is the object creating
knownImage from the video card. It also does a lock(this) when it
// clones the image from the video device to make knownImage.
{
Image myImage = knownImage.Clone() as Image;
}
sr = new StreamWriter(tempImagePath);
try
{
myImage.Save(sr.BaseStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
finally
{
sr.Close();
}
// the above is wrapped in another try finally that does:
myImage.Dispose();
The other issue is that the above file at tempImagePath is now in use and
cannot be written or deleted, even though I have disposed the streamwriter
and the image objects. However, in disposing the image I get an exception:
System.InvalidOperationException: The object is currently in use elsewhere.
at System.Drawing.Image.Dispose(Boolean disposing)
at System.Drawing.Image.Dispose()
at TvTunerVideoGrabber.MainForm.OnFtpTimerTick(Object sender, EventArgs
e) in c:\documents and settings\all users\documents\visual studio
projects\livevideocaptureandrecord\tvtunercapture\mainform.cs:line 612
Is there something I can look at or test the image before saving it so as
not to leave a file in use?