我有一个问题,我想将图像从 url 转换为 base64,但出现错误
public String ConvertImageURLToBase64(String url)
{
StringBuilder sb = new StringBuilder();
Byte[] vbyte = this.GetImage(url);
sb.Append(Convert.ToBase64String(vbyte, 0, vbyte.Length));
return sb.ToString();
}
private byte[] GetImage(string url)
{
Stream stream = null;
byte[] buf;
try
{
WebProxy myProxy = new WebProxy();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
stream = response.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
int len = (int)(response.ContentLength);
buf = br.ReadBytes(len);
br.Close();
}
stream.Close();
response.Close();
}
catch (Exception exp)
{
buf = null;
}
return (buf);
}