简体   繁体   中英

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

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM