Base64 encoded files are larger than the original. The advantage lies in not having to open another connection and make a HTTP request to the server for the image. This benefit is lost very quickly so there's only an advantage for large numbers of very tiny individual images.
However we may still need to convert an image to base64 on the way to display our data on HTML page. Assuming you have a class that represent all the necessary details, here is how you can have an additional property to the class that converts images to base64.
public string imageBase64
{
get
{
using (var client = new WebClient())
{
byte[] dataBytes = client.DownloadData(new Uri(image));
string encodedFileAsBase64 = Convert.ToBase64String(dataBytes);
return "data:image/jpeg;base64," + encodedFileAsBase64;
}
}
}
No comments:
Post a Comment