【发布时间】:2011-03-26 18:09:52
【问题描述】:
我正在使用数据库将客户的图像存储为字节。如何在 .aspx 页面上呈现这些图像?
【问题讨论】:
我正在使用数据库将客户的图像存储为字节。如何在 .aspx 页面上呈现这些图像?
【问题讨论】:
这可以通过将字节数组转换为 Base64 图像来轻松完成。
public string GetImageAsBase64String(byte[] bin)
{
if (bin != null)
{
return "<img src=\"data:image/jpeg;base64," + Convert.ToBase64String(bin) + "\">";
}
else
{
return null;
}
}
//usage, for demo purposes an uploaded image from a FileUpload Control
Label1.Text = GetImageAsBase64String(FileUpload1.FileBytes);
【讨论】:
两种解决方案。
构建处理程序页面。它将 ImageID/RowID 作为 GET 参数,并返回 mimetype image/jpeg 或 image/png 的数据。
使用wikipedia 中解释的 DATA uri 方案。
【讨论】:
说明可以在这里找到:http://www.dotnetcurry.com/ShowArticle.aspx?ID=129&AspxAutoDetectCookieSupport=1
在第 4 步中,但整篇文章值得一读。
【讨论】: