In this post, we will see how to resolve How to keep the MemoryStream from being closed automatically
Question:
I have a memory stream in a function like soI would like to use this MemoryStream by seeking to 0 and read again and again later. How can I achieve that?
If I am using CopyTo, then the new MemoryStream is kept open but is that hugely inefficient? As I understand, the CopyTo will read the whole backing memory and write to the new Stream, basically double the memory usage? The result I received from CopyTo is what I wanted, I am just not sure if that is the right way or if there are better alternatives
using var document = new PdfDocument(); var memoryStream = new MemoryStream(); document.Save(memoryStream, new GemBox.Pdf.PdfSaveOptions()); memoryStream.Seek(0, SeekOrigin.Begin); var newStream = new MemoryStream(); memoryStream.CopyTo(newStream); newStream.Seek(0, SeekOrigin.Begin); return newStream;
Sample Repro https://dotnetfiddle.net/mCZhug
Callstack at dispose time

Best Answer:
The reason why it’s closed is because the stream ends up being disposed with thePdfDocument.Dispose()
call, that is the whole point of that method.There are two ways how you can resolve your issue, first is to not call
PdfDocument.Dispose()
(in other words, remove the using
statement):PdfDocument.CloseStream
option to false
has the same effect as not calling PdfDocument.Dispose()
.If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com
Leave a Review