/// <summary> /// 把图像文件转化为字节数组 /// </summary> /// <param name="imageFile"></param> /// <returns></returns> public static byte[] Image2Bytes(string imageFile) { using (var ms = new MemoryStream()) { using (Image img = Image.FromFile(imageFile)) { img.Save(ms, GetImageFormat(imageFile)); return(StreamUtility.Stream2Bytes(ms)); } } }
/// <summary> /// 把字节数组转化为图像对象 /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static Image Bytes2Image(byte[] bytes) { try { return(Image.FromStream(StreamUtility.Bytes2Stream(bytes))); } catch { return(null); } }
/// <summary> /// 把图像转化为字节数组 /// </summary> /// <param name="img"></param> /// <returns></returns> public static byte[] Image2Bytes(Image img) { if (img == null) { return(null); } using (Stream s = new MemoryStream()) { img.Save(s, ImageFormat.Jpeg); return(StreamUtility.Stream2Bytes(s)); } }