How to save stream as image and store the image in temp files?
Asked
Active
Viewed 7.9k times
5 Answers
38
Try
Image img = System.Drawing.Image.FromStream(myStream);
img.Save(System.IO.Path.GetTempPath() + "\\myImage.Jpeg", ImageFormat.Jpeg);
Filip Ekberg
- 35,726
- 18
- 122
- 180
Unmesh Kondolikar
- 9,136
- 3
- 33
- 51
-
-
2Yes .. But I assumed that banupriyavijay wants to so something with the image in the application. Otherwise it would be just a file copy operation which can be done using System.IO.File.Copy(). – Unmesh Kondolikar Sep 06 '10 at 12:40
-
3When i write this line Image img = System.Drawing.Image.FromStream(stream); it gives me an error "Parameter is not valid." – Chirag Sep 23 '15 at 09:46
-
this solution has issues if you are trying to override an image. I am getting a A Generic error occurred in GDI+ – Juan Zamora Oct 03 '17 at 01:33
6
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.
VinayC
- 44,892
- 5
- 56
- 69
-
2
-
FileStream does not contain a definition for Create!! Moreover the stream which i use as input is System.IO.Stream and I want to store this IO stream to image in temp files. – banupriya Sep 06 '10 at 12:14
-
Sorry for that - I mean to say File.Create and not FileStream.Create. Will edit the answer. Source stream can be of any stream. – VinayC Sep 06 '10 at 12:48
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 ;)
Community
- 1
- 1
1
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.
Filip Ekberg
- 35,726
- 18
- 122
- 180
0
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);
ickydime
- 1,011
- 9
- 21