using System.Drawing.Imaging; using Image = System.Drawing.Image; namespace Hopetry.App.Common; public class ImageHelper { /// /// 图片转换成字节流 /// /// 要转换的Image对象 /// 转换后返回的字节流 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; } /// /// 图片转换成字节流 /// /// 要转换的Image对象 /// 转换后返回的字节流 public static byte[] ImgToByt(Image img) { MemoryStream ms = new MemoryStream(); byte[] imagedata = null; img.Save(ms, ImageFormat.Jpeg); imagedata = ms.GetBuffer(); return imagedata; } }