简体   繁体   English

如何显示图像 <img> 从数据库中存储为基础64 Stirng?

[英]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? 我该怎么做?

  1. Add a GenericHandler to your web site named DBImage.ashx 将GenericHandler添加到名为DBImage.ashx的网站
  2. Your img src attributes should point to something like DBImage.ashx?key=[dbkey] 您的img src属性应指向DBImage.ashx?key = [dbkey]之类的东西
  3. 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.

相关问题 如何显示图像 <img> 名称存储在数据库中的标签 - how to display image in a <img> tag whose name is stored in database 如何将 URL 传入<img>来自 Webapi 的标签以接受 base64 的图像? - How to pass URL into <img> tag from Webapi to accept image of base64? 如果图像ID未知,如何显示数据库中的图像(存储为BLOB)? - How to display image from database (stored as BLOB), if the image ID is not known? 如何在Aspose Word文档中显示SQL Server数据库图像base64 - How to display SQL Server database image base64 in Aspose Word Document 使用JavaScript在img中使用C#代码显示生成的gif; base64图像 - Display a generated gif;base64 image using C# code behind in img using JavaScript 无法显示从Base64 BitmapImage创建的图像 - Unable to display Image created from Base64 BitmapImage 将base64字符串图像绑定到存储在Windows Phone 8.1的SQLite数据库中的列表视图中 - Binding base64 String Image into List view Stored in SQLite Database in windows phone 8.1 C#到JS:如何以base64(RawBytes)图像格式附加img src? - C# to JS: How to append img src with base64 (RawBytes) image format? 如何在img标签中显示图像? - How to display image in img tag? MVC视图中未显示base64图像 - base64 image not display in mvc view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM