【问题标题】:ByteArray to asp Image [duplicate]ByteArray到asp图像[重复]
【发布时间】:2012-12-05 18:13:32
【问题描述】:

可能重复:
How to show a image in database in the image control of 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 的定义)

【问题讨论】:

  • 你试过什么?
  • 抱歉,我想知道我是如何隔开并忘记发布那部分的。太沮丧了。
  • 您是否尝试过 System.Drawing.Image 而不是仅使用 Image?可能存在命名冲突。
  • @Nick Bray 我试过了,但是返回类型有冲突。
  • 然后将返回类型设为System.Drawing.Image

标签: c# asp.net image


【解决方案1】:

如果你使用 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")'/>

对于网络表单,请参阅

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

【讨论】:

  • 好吧,如果归根结底,我可能会尝试将其转换为 MVC。我很感激这些信息!
【解决方案2】:

您可能会遇到一些问题:

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

  2. 您无法在图像上释放流。删除 using 块。 https://stackoverflow.com/a/13696705/64262

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      相关资源
      最近更新 更多