Convert online image to base64 with C#

  • December 12, 2010
  • James Skemp
  • Internet

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 = "";

Uri uri = new Uri(url);

WebClient client = new WebClient();
byte[] imageBytes = client.DownloadData(uri);

string base64String = Convert.ToBase64String(imageBytes);


("<img src='data:image/jpg;base64," + "' />").Length.Dump();


The length dumps are given to get an idea of what size would be added to a file if added inline.