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: JLHua Silicone Multi-Use Cartoon Cat Trivet Mat (set of 6 Pack) Insulated Flexible Durable Non Slip Hot Pads and Coasters Cup Mats
- Review: Tash-Kalar: Arena of Legends
- Review: Babymoov Dream Belt – Maternity Sleep Support & Cushion for Ultimate Comfort during Pregnancy
- Review: AmazonBasics PlayStation 4 Controller Charging Cable
- Review: Galaxy Trucker
Support This Site
If my blog was helpful to you, then please consider visiting my Amazon Wishlist.