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


public static class BitmapHelper
{

    public static Stream ToStream(this Bitmap bitmap)
    {
        MemoryStream ms = new MemoryStream();
        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        ms.Seek(0, SeekOrigin.Begin);
        return ms;
    }


    public static byte[] Bitmap2Byte(this Bitmap bitmap)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, ImageFormat.Jpeg);
            byte[] data = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(data, 0, Convert.ToInt32(stream.Length));
            return data;
        }
    }

}

