NET+EF+MVC开发中,有时候需要将图片转为base64字符串,下边介绍一个完整的方法,供开发时参考:
#region 图片转为base64字符串
public static string ImageToBase64String(string imagePath)
{
try
{
Bitmap bitmap = new Bitmap(imagePath);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Jpeg);
byte[] bytes = new byte[ms.Length];
ms.Position = 0;
ms.Read(bytes, 0, (int)ms.Length);
ms.Close();
return Convert.ToBase64String(bytes);
}
catch (Exception ex)
{
throw new ApplicationException("图片转base64字符串时异常", ex);
}
}
#endregion 图片转为base64字符串
| 留言与评论(共有 0 条评论) “” |