Image和byte[]互相转换 和 Image 与 base64互相转换
2022-08-12 13:50:11
#region byte[]和Image互相转换
/// <summary> /// 字节流转图片 /// </summary> /// <param name="Bytes">字节数组</param> /// <returns>图片</returns> private static Bitmap BytesToImage(byte[] bytes) { try { using (MemoryStream ms = new MemoryStream(bytes)) { return new Bitmap(ms); } } catch (Exception ex) { return null; } } public static Image ByteToImage(byte[] byImage) { if (byImage.Length == 0) return null; MemoryStream ms = new MemoryStream(byImage); Image image = Image.FromStream(ms); return image; } /// <summary> /// 字节数组转换为图片 /// </summary> /// <param name="buffer">字节数组</param> /// <returns>图片</returns> public Image BytesToImage(Byte[] buffer) { var ms = new MemoryStream(buffer, 0, buffer.Length); var bf = new BinaryFormatter(); object obj = bf.Deserialize(ms); ms.Close(); return (Image)obj; } /// <summary> /// 图片转字节数组 /// </summary> /// <param name="Picture"></param> /// <returns></returns> public static byte[] ImageToByte(Image Pic) { MemoryStream ms = new MemoryStream(); if (Pic == null) return new byte[ms.Length]; Pic.Save(ms, ImageFormat.Jpeg); byte[] BPicture = new byte[ms.Length]; BPicture = ms.GetBuffer(); return BPicture; }
/// <summary> /// 图片转换为字节数组 /// </summary> /// <param name="image">图片</param> /// <returns>字节数组</returns> public byte[] ImageToByte(Image image)
{
MemoryStream ms = new MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] bt = ms.GetBuffer();
return bt;
} /// <summary> /// GDI+中发生一般性错误的解决方法:释放图片资源 /// </summary> /// <param name="image"></param> /// <returns></returns> private static byte[] GetBytes(Image image) { try { if (image == null) return null; using (Bitmap bitMap = new Bitmap(image)) { using (MemoryStream stream = new MemoryStream()) { bitMap.Save(stream,ImageFormat.Jpeg); return stream.GetBuffer(); } } } finally { if (image != null) { image.Dispose(); image = null; } } }
#endregion #region Image 与 base64互相转换 /// <summary> /// Image 转成 base64 /// </summary> /// <param name="fileFullName"></param> public static string ImageToBase64E(Image img) { try { Bitmap bmp = new Bitmap(img); MemoryStream ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Jpeg); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); return Convert.ToBase64String(arr); } catch (Exception ex) { return null; } } public static string ImageToBase64E(string url) { return ImageToBase64E(UrlToImage(url)); } /// <summary> /// base64转图片 /// </summary> /// <param name="base64"></param> public static Image Base64ToImage(string base64) { try { base64 = base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "").Replace("data:image/jpng;base64,", "").Replace("data:image/jpng;base64;", "");//将base64头部信息替换 byte[] bytes = Convert.FromBase64String(base64); MemoryStream memStream = new MemoryStream(bytes); Image mImage = Image.FromStream(memStream); return mImage; //Bitmap bp = new Bitmap(mImage); //string path = @"D:\uploads"; //if (!Directory.Exists(path)) //{ // Directory.CreateDirectory(path); //} //DateTime now = DateTime.Now; //string fileName = "images"; //string filePath = path + "\\" + fileName; //bp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);//注意保存路径 } catch (Exception ex) { LogerHelper.CreateLogTxt($"base64转图片失败,错误详情: {ex.Message}"); throw; } } #endregion
/// <summary> /// 图片转换为字节数组 /// </summary> /// <param name="image">图片</param> /// <returns>字节数组</returns> public byte[] ImageToBytes(Image image) { var ms = new MemoryStream(); var bf = new BinaryFormatter(); bf.Serialize(ms, image); ms.Close(); return ms.ToArray(); }
欢迎加群交流 QQ群 : 830426796