2012-12-05 35 views
0

可能重复:
How to show a image in database in the image control of Asp.net?ByteArray的ASP图像

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

你试过了什么? –

+0

对不起,idk我如何间隔出来,忘了张贴那部分。太沮丧了。 –

+0

您是否尝试使用System.Drawing.Image而不是仅使用Image?可能存在命名冲突。 –

回答

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")'/> 

的web表单见

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

+0

那么如果它归结为它,我可能会尝试并将其转换为MVC。我很欣赏这些信息! –

1

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

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

  2. 您不能在图像上放置流。删除使用块。 https://stackoverflow.com/a/13696705/64262

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