System.Drawing.Image for c # stream

I have System.Drawing.Image in my program. The file is not in the file system stored in memory. I need to create a stream from it. How can i do this?

+49
Nov 03 '09 at 16:29
source share
2 answers

Try the following:

 public static Stream ToStream(this Image image, ImageFormat format) { var stream = new System.IO.MemoryStream(); image.Save(stream, format); stream.Position = 0; return stream; } 

Then you can use the following:

 var stream = myImage.ToStream(ImageFormat.Gif); 

Replace GIF with any format suitable for your scenario.

+101
Nov 03 '09 at 16:32
source share
— -

Use memory stream

 using(MemoryStream ms = new MemoryStream()) { image.Save(ms, ...); return ms.ToArray(); } 
+6
Nov 03 '09 at 16:31
source share



All Articles