using System.Drawing.Imaging;
using Image = System.Drawing.Image;

namespace Hopetry.App.Common;

public class ImageHelper
{
    /// <summary>
    /// 图片转换成字节流
    /// </summary>
    /// <param name="img">要转换的Image对象</param>
    /// <returns>转换后返回的字节流</returns>
    public static Stream ImgToStream(Image img)
    {
        byte[] imgByte = ImgToByt(img);
        //img.Save(ms, img.RawFormat);//System.Drawing.Imaging.ImageFormat.Jpeg
        Stream stream = new MemoryStream(imgByte);
        return stream;
    }

    /// <summary>
    /// 图片转换成字节流
    /// </summary>
    /// <param name="img">要转换的Image对象</param>
    /// <returns>转换后返回的字节流</returns>
    public static byte[] ImgToByt(Image img)
    {
        MemoryStream ms = new MemoryStream();
        byte[] imagedata = null;
        img.Save(ms, ImageFormat.Jpeg);
        imagedata = ms.GetBuffer();
        return imagedata;
    }
}