System.Drawing.Image to stream C#

Viewed 117204

I have a System.Drawing.Image in my program. The file is not on the file system it is being held in memory. I need to create a stream from it. How would I go about doing this?

4 Answers
public static Stream ToStream(this Image image)
{
     var stream = new MemoryStream();

     image.Save(stream, image.RawFormat);
     stream.Position = 0;

     return stream;
 }

Using File Stream

 public Stream ToStream(string imagePath)
    {
    Stream stream=new FileStream(imagePath,FileMode.Open);
    return stream;
    }
Related