C# 将图像转换为数据:image/png;base64 用于网页显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9728424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting image into data:image/png;base64 for web page disaplay
提问by Maxim V. Pavlov
If one visits jQuery-File-Upload Demo pageand 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:
如果访问jQuery-File-Upload Demo 页面并尝试上传图像,然后查看 JSON 响应,他会注意到上传图像的预览以一种格式返回:
"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?
我怎样才能在 C# 中为同一个演示实现 ASP.NET 后端?
采纳答案by dash
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 exampleabout how to set the src of an ASP.Net image to be the base64 encoded data you see above.
在那个答案中是关于如何将 ASP.Net 图像的 src 设置为您在上面看到的 base64 编码数据的示例。
It effectively boils down to setting the src of an ASP:Image control as follows:
它实际上归结为设置 ASP:Image 控件的 src 如下:
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!
请记住根据图像更改内容类型!

