How to save stream as image and开发者_运维问答 store the image in temp files?
Try
Image img = System.Drawing.Image.FromStream(myStream);
img.Save(System.IO.Path.GetTempPath() + "\\myImage.Jpeg", ImageFormat.Jpeg);
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.
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 ;)
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.
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);
精彩评论