Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public String ConvertImageURLToBase64(String url)
{
StringBuilder _sb = new StringBuilder();
Byte[] _byte = this.GetImage(url);
_sb.Append(Convert.ToBase64String(_byte, 0, _byte.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);
}
@talhakaanozkan
Copy link

talhakaanozkan commented Mar 20, 2020

{"'System.Net.FileWebRequest' türündeki nesne 'System.Net.HttpWebRequest' türüne atılamadı."}
not working.

@malkitsingh
Copy link
Author

malkitsingh commented Mar 21, 2020

hi @talhakaanozkan
This code was written long back, make sure you are importing all the required dependencies.

@rahulbagal
Copy link

rahulbagal commented Sep 24, 2020

Thanks. Its a quick helpful reference. working fiddle - https://dotnetfiddle.net/D22EBb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment