Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy MemoryStream to FileStream and save the file?

I don't understand what I'm doing wrong here. I generate couple of memory streams and in debug-mode I see that they are populated. But when I try to copy MemoryStream to FileStream in order to save the file fileStream is not populated and file is 0bytes long (empty).

Here is my code

if (file.ContentLength > 0) {     var bytes = ImageUploader.FilestreamToBytes(file); // bytes is populated      using (var inStream = new MemoryStream(bytes)) // inStream is populated     {         using (var outStream = new MemoryStream())         {             using (var imageFactory = new ImageFactory())             {                 imageFactory.Load(inStream)                             .Resize(new Size(320, 0))                             .Format(ImageFormat.Jpeg)                             .Quality(70)                             .Save(outStream);             }                          // outStream is populated here                          var fileName = "test.jpg";              using (var fileStream = new FileStream(Server.MapPath("~/content/u/") + fileName, FileMode.CreateNew, FileAccess.ReadWrite))             {                 outStream.CopyTo(fileStream); // fileStream is not populated             }         }     } } 
like image 529
Stan Avatar asked Sep 12 '13 13:09

Stan


People also ask

How do you save on MemoryStream?

Save MemoryStream to a String StreamWriter sw = new StreamWriter(memoryStream); sw. WriteLine("Your string to Memoery"); This string is currently saved in the StreamWriters buffer. Flushing the stream will force the string whose backing store is memory (MemoryStream).

How do I reuse MemoryStream?

You can re-use the MemoryStream by Setting the Position to 0 and the Length to 0. By setting the length to 0 you do not clear the existing buffer, it only resets the internal counters.

What is the difference between MemoryStream and FileStream?

As the name suggests, a FileStream reads and writes to a file whereas a MemoryStream reads and writes to the memory. So it relates to where the stream is stored.


2 Answers

You need to reset the position of the stream before copying.

outStream.Position = 0; outStream.CopyTo(fileStream); 

You used the outStream when saving the file using the imageFactory. That function populated the outStream. While populating the outStream the position is set to the end of the populated area. That is so that when you keep on writing bytes to the steam, it doesn't override existing bytes. But then to read it (for copy purposes) you need to set the position to the start so you can start reading at the start.

like image 134
SynerCoder Avatar answered Oct 05 '22 18:10

SynerCoder


If your objective is simply to dump the memory stream to a physical file (e.g. to look at the contents) - it can be done in one move:

System.IO.File.WriteAllBytes(@"C:\\filename", memoryStream.ToArray()); 

No need to set the stream position first either, since the .ToArray() operation explicitly ignores that, as per @BaconBits comment below https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream.toarray?view=netframework-4.7.2.

like image 41
James Harcourt Avatar answered Oct 05 '22 19:10

James Harcourt