Save stream as image

How to save stream as image and store the image in temp files?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try

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

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

Method 2

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.

Method 3

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 😉

Method 4

Have a look at the Bitmap Class. There’s a constructor overload that takes a Stream as parameter and there’s a method called Save which you can use to save it as a file.

Method 5

For windows phone 8.1 I found the following to work well. Create your TempStorageFile at whatever path you would like and then pass in your image’s stream like so:

var fileStream = await TempStorageFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
            await RandomAccessStream.CopyAsync(imageStream, fileStream);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x