NullReferenceException with Image.Save

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
Hello Rick,
I made a simple test in my XP box, I could correctly make the image save to
the writer stream and have it disposed.
From the call stack and exception, seems you haven't got "myImage"
completely created. Since you grab this image from video memory, I feel a
bit unsure if you get the video mem block correctly cloned. Before we go
further, could you show more details on what you were doing before
Image myImage = knownImage.Clone() as Image;


Thanks,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
Hello Rhett,
Thanks for your reply. The software that was creating the image is from a
sample from the code project. (DirectX.Capture modified to have an
ISampleGrabber filter added). The handler for the ISampleGrabber BufferCB
call was writing to an array which was used to construct the image. I added
some thread locking to ensure coherency of this array as well as a logic
change for checking the size of the array and my issue is now resolved.

Thanks again,
Rick


Rhett Gong said:
Hello Rick,
I made a simple test in my XP box, I could correctly make the image save to
the writer stream and have it disposed.
From the call stack and exception, seems you haven't got "myImage"
completely created. Since you grab this image from video memory, I feel a
bit unsure if you get the video mem block correctly cloned. Before we go
further, could you show more details on what you were doing before
Image myImage = knownImage.Clone() as Image;


Thanks,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
It is glad to hear that the problem has been resolved. Thanks for your
feedback. See you next time. :)


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
Hello Rick and Rhett,


I don't know if you are still looking at this thread, but I have exactly the same problem. I use the same example of Code Project to grab a frame, then I do some processing on it in C++ using dllimport and finally I callmy

Image.Save(sr.BaseStream,
System.Drawing.Imaging.ImageFormat.Jpeg);

As beffore sometimes it work :
1. Well
2. Changes the collor of the images, switchs RGB as wanted.
3. Have the sme problem that you had before.

System.InvalidOperationException: The object is currently in use elsewhere.
at System.Drawing.Image.Dispose(Boolean disposing)
at System.Drawing.Image.Dispose()

I tried to solve the problem using your previous messages but I still have zthe bag..... any idea.

Thanks



Alfonso
 
Back
Top