0

可能重复:
如何在 Asp.net 的图像控件中显示数据库中的图像?

我正在从数据库中返回产品列表。在这个列表中,我将数据库图像保存为字节数组。目前我在一个页面上显示大约 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 的定义)

4

3 回答 3

2

If your using Mvc its rather simple

Simply write an action such as the following

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");
}

See This Link For Mime types

http://www.webmaster-toolkit.com/mime-types.shtml

I highly suggest adding a column to your files table to store mime types (determine this on upload)

And in your view put down an image like this

<img src='@Url.Action("GalleryMediumThumb")'/>

for webforms see

How to show a image in database in the image control of Asp.net?

于 2012-12-05T18:31:14.147 回答
1

您可能在这里遇到一些问题:

  1. 您正在引用一个 asp.net Image 对象 ( System.Web.UI.WebControls.Image) 而不是您想要的 GDI+System.Drawing.Image对象。澄清你的命名空间。

  2. 您不能在图像上处理流。删除 using 块。https://stackoverflow.com/a/13696705/64262

  3. 您需要将结果流写入响应流(首先不需要将其转换为图像),然后从<img>标记中引用该端点。

于 2012-12-05T18:48:44.820 回答
0

通用 ASP.NET 处理程序 (*.ashx) 可以从数据库加载图像并将其作为普通图像流式传输到浏览器。

愿此http://coffeedrivendevelopment.net/2012/10/26/sharing-image-resources-between-wpf-and-asp-net/对您有所帮助。它是关于资源中的图像,但方法是相同的。

于 2012-12-05T18:27:44.893 回答