/// <summary>
/// Base64转图片
/// </summary>
/// <param name="data"></param>
public Image Base64ToImage(string data)
{
data = data.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
byte[] bytes = Convert.FromBase64String(data);
MemoryStream memStream = new MemoryStream(bytes);
Image mImage = Image.FromStream(memStream);
Bitmap bp = new Bitmap(mImage);
bp.Save(DateTime.Now.ToString("yyyyMMddHHss") + ".png", System.Drawing.Imaging.ImageFormat.Png);//注意保存路径
return bp;
}
/// <summary>
/// 图片转Base64
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
public string ImageToBase64(Image img)
{
try
{
Bitmap bmp = new Bitmap(img);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.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;
}
}
标签:Base64,image,base64,ms,互转,new,data,图片
From: https://www.cnblogs.com/jnmcok/p/17781991.html