Convert System.Drawing.Image to stream

Possible duplicate:
System.Drawing.Image for c # stream

How can I convert the System.Drawing.Image file to a stream?

+7
source share
3 answers

You can “save” the image to the stream.

If you need a stream that can be read elsewhere, just create a MemoryStream :

 var ms = new MemoryStream(); image.Save(ms, ImageFormat.Png); // If you're going to read from the stream, you may need to reset the position to the start ms.Position = 0; 
+28
source

Add a link to System.Drawing and include the following namespaces:

 using System.Drawing; using System.Drawing.Imaging; using System.IO; 

And something like this should work:

 public Stream GetStream(Image img, ImageFormat format) { var ms = new MemoryStream(); img.Save(ms, format); return ms; } 
+2
source
 MemoryStream memStream = new MemoryStream(); Image.Save(memStream, ImageFormat.Jpeg); 

How did I do this when I needed to stream the image from a web server. (Note that of course you can change the format).

+1
source

All Articles