可能重复:
How to show a image in database in the image control of Asp.net?
我从数据库返回产品列表。在此列表中,我将数据库图像保存为bytearrays。目前我在一个页面上显示约30个产品,我想在产品信息旁边添加他们的图像。最简单的方法是什么?
我有什么:
public Image PopulatePicture()
{
Image newImage;
//Read image data into a memory stream
using (MemoryStream ms = new MemoryStream(ImageByteArray, 0, ImageByteArray.Length))
{
ms.Write(ImageByteArray, 0, ImageByteArray.Length);
//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true);
}
return newImage;
}
我在Image.FromStream上有错误(System.Web.UI.WebControls不包含FromStream的定义)
答案 0 :(得分:2)
如果您使用Mvc则相当简单
只需编写如下操作
即可public FileContentResult Image()
{
//Get the Byte Array for your image
byte[] image = FilesBLL.GetImage();
//Return a jpg
//Note the second parameter is the files mime type
return File(image, "image/jpeg");
}
请参阅此链接适用于Mime类型
http://www.webmaster-toolkit.com/mime-types.shtml
我强烈建议在文件表中添加一列来存储mime类型(在上传时确定)
在你的视图中放下一个像这样的图像
<img src='@Url.Action("GalleryMediumThumb")'/>
for webforms参见
How to show a image in database in the image control of Asp.net?
答案 1 :(得分:1)
你可能会遇到一些问题:
您正在引用一个asp.net图像对象(System.Web.UI.WebControls.Image
),而不是您想要的GDI + System.Drawing.Image
对象。澄清您的命名空间。
您无法在图像上处理流。删除使用块。 https://stackoverflow.com/a/13696705/64262
您需要将结果流写入响应流(无需首先将其转换为图像),然后从<img>
标记引用该端点。
答案 2 :(得分:0)
通用ASP.NET处理程序(* .ashx)可以从数据库加载图像并将其作为普通图像流式传输到浏览器。
这个http://coffeedrivendevelopment.net/2012/10/26/sharing-image-resources-between-wpf-and-asp-net/可以帮助你。它是关于资源中的图像,但方式是相同的。