将图像转换为数据:image/png;base64 用于网页显示 [英] Converting image into data:image/png;base64 for web page disaplay

查看:38
本文介绍了将图像转换为数据:image/png;base64 用于网页显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果访问 jQuery-File-Upload Demo 页面 并尝试上传一张图片,然后会看 JSON 响应,他会注意到上传图片的预览是以一种格式返回的:

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.

如何在 C# 中实现相同演示的 ASP.NET 后端?

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!"

在那个答案中是一个示例,关于如何将 ASP.Net 图像的 src 设置为 base64 编码你在上面看到的数据.

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.

它实际上归结为如下设置 ASP:Image 控件的 src:

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!

这篇关于将图像转换为数据:image/png;base64 用于网页显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆