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

51,208

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!

Share:
51,208
Maxim V. Pavlov
Author by

Maxim V. Pavlov

Part of the Universe and beyond.

Updated on August 12, 2022

Comments

  • Maxim V. Pavlov
    Maxim V. Pavlov over 1 year

    If one visits jQuery-File-Upload Demo page and will try to upload an image, and then will look at the JSON response, he would notice that a preview of an uploaded image is returned in a format:

    "thumbnail_url":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAI...
    

    As far as I understand, an images is getting transformed into string and sent back to the client.

    How can I do it in C# to impelement ASP.NET back end for the same demo?