I have some code that does
MemoryStream ms = new MemoryStream();
...
return Image.FromStream(ms);
It fails in very eclectic ways since the Image object does not hold a ref to the stream, so it can get disposed if the GC kicks in which results in GDI+ errors.
How do I work around this (without saving the stream to disk, or altering my method sigs) ?
This seems highly unlikely to me – it would cause a problem for almost any use of
Image.FromStream.It seems more likely to me that something’s disposing of your
MemoryStream, which it shouldn’t.Could you provide a short but complete program which demonstrates the problem? Forcing garbage collection should make it relatively easy to reproduce – you could even create your own class deriving from
MemoryStreamwith a finalizer to show whether or not it really is being collected (well, finalized at least).