Convert online image to base64 with C#
The following code will convert an online image to a base64 string.
The code was specifically written for use in LINQPad, hence the use of Dump().
String url = "http://ecx.images-amazon.com/images/I/51MVXV3QQ6L._SL500_SS150_.jpg";
Uri uri = new Uri(url);
WebClient client = new WebClient();
byte[] imageBytes = client.DownloadData(uri);
string base64String = Convert.ToBase64String(imageBytes);
base64String.Dump();
base64String.Length.Dump();
("<img src=‘data:image/jpg;base64," + “’ />”).Length.Dump();
client.Dispose();
The length dumps are given to get an idea of what size would be added to a file if added inline.
Search
Links of Note
Support This Site
If my blog was helpful to you, then please consider visiting my Amazon Wishlist.