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.
- Review: Empires of EVE: A History of the Great Wars of EVE Online
- Review: Ghost in the Shell README: 1995-2017
- Review: Fitbit Charge 2 Bands,Fitbit Bands Milanese Loop Stainless Steel Metal Replacement Bracelet Strap, Wristbands Accessories for Charge 2 with Stronger Magnetic Clasp
- Review: Wickedly Prime Organic Roasted Seaweed Snacks, Korean Kalbi
- Review: 5-Minute Dungeon
Support This Site
If my blog was helpful to you, then please consider visiting my Amazon Wishlist.