.NET 网络图片地址转换为Base64
1.地址获取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.Image转换为Base64
Image img = UrlToImage("https://www.xxx.com/xxxx/xxx.png");
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();
string base64 = Convert.ToBase64String(arr);