1.通过Url获取到Image格式的文件
public static Image UrlToImage(string url)
{
WebClient mywebclient = new WebClient();
byte[] Bytes = mywebclient.DownloadData(url);
using (MemoryStream ms = new MemoryStream(Bytes))
{
Image outputImg = Image.FromStream(ms);
return outputImg;
}
}
2、再将Iamge文件转为Base64字符串的文件
Image img = UrlToImage(member.IconUrl);
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();
member.IconUrl = Convert.ToBase64String(arr);
3、前端将base64的图片字符串转换为图片
CSS中使用:background-image: url("data:image/png;base64,iVBORw0KGgo=...");
HTML中使用:<img src="data:image/png;base64,iVBORw0KGgo=..." />
原文地址:https://www.cnblogs.com/hezhengyi/p/11172197.html