[英]How to display image in <img> from database where it is stored as base 64 Stirng?
I have stored image files in format base64String in database and need to render that image in a html <img>
tag. 我已将图像文件以base64String格式存储在数据库中,并且需要在html <img>
标签中呈现该图像。
How do I do it? 我该怎么做?
In the generic handler's ProcessRequest you will 在通用处理程序的ProcessRequest中,您将
a) retrieve the base64 string from the database based on the query string dbkey a)根据查询字符串dbkey从数据库中检索base64字符串
b) convert the base64 to a byte array using Convert.FromBase64String() b)使用Convert.FromBase64String()将base64转换为字节数组
c) set the response content type to the appropriate mime type (like image/jpeg) c)将响应内容类型设置为适当的mime类型(例如image / jpeg)
d) write the byte array to the response stream d)将字节数组写入响应流
That should do it. 那应该做。
You can use base64 string directly to display image without any kind of conversion 您可以直接使用base64字符串来显示图像,而无需进行任何类型的转换
<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." />
make sure your base64 string have data:image/png;base64
确保您的base64字符串具有data:image/png;base64
if not try this function to simply append it 如果没有尝试使用此功能简单地附加它
public string GetImageSrc(string base64Src)
{
return "data:image/png;base64," + base64Src;
}
Demo : http://jsfiddle.net/casiano/xadvz/ 演示: http : //jsfiddle.net/casiano/xadvz/
Try this: 尝试这个:
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
May be this would help you 也许这会帮助你
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
Image image = Image.FromStream(ms, true);
return image;
}
}
after that you can easily call Image.Save(filePath);
之后,您可以轻松调用Image.Save(filePath);
to save the image and call that image to your HTML. 保存图像并将该图像调用到您的HTML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.