Save stream as image

Your stream (image) is stream in the code below.

using (Stream output = new FileStream ("mycat.jpg"))
{
    byte[] buffer = new byte[32*1024];
    int read;

    while ( (read=stream.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, read);
    }
}

This code is copyrighted by Jon Skeet My contribution is the name of the file ;)


var tempFile = Path.GetTempFileName();
using (var fs = File.Create(tempFile))
{
   source.copyTo(fs);
}

where source is source stream. Now your source stream is saved at temp location (given by tempFile). Note that file name extension will be TMP.


Try

Image img = System.Drawing.Image.FromStream(myStream);

img.Save(System.IO.Path.GetTempPath() + "\\myImage.Jpeg", ImageFormat.Jpeg);

Tags:

C#

Asp.Net