Converting image into data:image/png;base64 for web page disaplay Converting image into data:image/png;base64 for web page disaplay asp.net asp.net

Converting image into data:image/png;base64 for web page disaplay


I remember reading an answer to a question a while back by the very competent competent_tech and thinking "I never knew you could do that!"

In that answer is an example about how to set the src of an ASP.Net image to be the base64 encoded data you see above.

It effectively boils down to setting the src of an ASP:Image control as follows:

imgCtrl.Src = @"data:image/gif;base64," + Convert.ToBase64String(File.ReadAllBytes(Server.MapPath(@"/images/your_image.gif")));

Remember to change the content type depending on the image!