如何在asp.net显示数据库中的图像 [英] How to display image from database in asp.net
本文介绍了如何在asp.net显示数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据库中保存的图像,并想显示它在asp.net中,我有字节数组和MIME和大小...
I have saved image in database and wanna show it in asp.net ,I have byte array and MIME and size...
var da = new SqlDataAdapter("GetBlob", sqlConnection);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@BlobId", blobDto.BlobId);
var ds = new DataSet();
da.Fill(ds, "GetBlob");
var dr = ds.Tables[0].Rows[0];
var byteArray = (byte[]) dr["BlobData"];
string MIME=dr["MIME"]; e.g .jpg
请帮助我,我怎么能显示此图像。
Please help me how can I show this image
推荐答案
您可以将您的字节数组为Base64 EN codeD字符串,并将它传递给一些 IMG
标签为的src
属性。在的src
属性为的base64 EN $ C $光盘映像格式是 - 数据: conentType 的; BASE64, theBase64string
You can convert your byte array to base64 encoded string and pass it to some img
tag as src
attribute. The src
attribute format for base64 encoded image is - data:conentType;base64,theBase64string.
要转换成字节数组的base64字符串使用Convert.ToBase64String(字节阵列);
To convert a byte array to base64 string use Convert.ToBase64String(byteArray);
举例C#:
byte[] bytes = new byte[]{ }; // .. Get it from database
string contentType = "image/jpeg"; // .. Get it from database
string imageSrc = string.Format("data:{0};base64,{1}",
contentType, Convert.ToBase64String(bytes));
在HTML:
<img src="@imageSrc" />
// Or
<img src="<%= imageSrc =>" />
// Depending on your view engine
这篇关于如何在asp.net显示数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文