44

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
            }
        }
    }
}
Stan
  • 22,856
  • 45
  • 148
  • 231
  • Are you sure outStream contains the image data? I think the problem in .Save(outStream); can you please post the call stack. – Bassam Alugili Sep 12 '13 at 13:57
  • 1
    @BassamAlugili na man, I checked in debug-mode. It's populated. `outStream.Position = 0` was the problem, I needed to set it. – Stan Sep 12 '13 at 14:01

3 Answers3

64

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.

SynerCoder
  • 11,621
  • 4
  • 46
  • 78
  • I'm wondering why he need this Position = 0; he just created the outStream and not used; Why should he get a wrong position? – Bassam Alugili Sep 12 '13 at 14:09
  • 7
    @BassamAlugili He used the outsteam when saving the file using the imageFactory. That function populated the outsteam. 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 doesnt 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. – SynerCoder Sep 12 '13 at 14:12
  • Thanks very useful info. – Bassam Alugili Sep 12 '13 at 14:15
18

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.

James Harcourt
  • 4,916
  • 4
  • 16
  • 35
  • 2
    You don't need `memoryStream.Position = 0;`. [MemoryStream.ToArray() explicitly ignores Position](https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream.toarray?view=netframework-4.7.2). – Bacon Bits Oct 30 '18 at 14:17
4

Another alternative to CopyTo is WriteTo.

Advantage:

No need to reset Position.

Usage:

outStream.WriteTo(fileStream);                

Function Description:

Writes the entire contents of this memory stream to another stream.

Fabian Bigler
  • 8,858
  • 4
  • 35
  • 61