/// <summary>
/// 网络图片转换为Base64格式
/// </summary>
/// <param name="urlAddress">图片在线网址</param>
/// <returns></returns>
public static string ImageToBase64ImageFormat(string urlAddress)
{
try
{
Uri url = new Uri(urlAddress);
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();
Image image = Image.FromStream(webResponse.GetResponseStream());
if (image == null)
{
return "";
}
Bitmap myImage = new Bitmap(image);
MemoryStream ms = new MemoryStream();
myImage.Save(ms, image.RawFormat);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return "data:image/"+ image.RawFormat.ToString().ToLower() + ";base64," + Convert.ToBase64String(arr);
}
catch
{
return string.Empty;
}
}