如何在MemoryStream中显示图像?

Jag*_*agd 1 c# asp.net memorystream image

我有一些图像(主要是png和jpg),我存储在SQL Server 2008的FileStream中.我能够检索所述图像并将它们存储在MemoryStream中.

我有一个网页,我需要在MemoryStream中显示图像.有没有什么方法可以在HTML图像标记内显示MemoryStream的内容(甚至更好,在ASP.NET图像控件中)?

Jas*_*ley 10

是,

  1. 将图像源指向ashx处理程序
  2. 让处理程序从数据库中查询图像
  3. 将字节写入响应流并设置内容类型

HTML

<img src="loadimage.ashx?id=..."/>
Run Code Online (Sandbox Code Playgroud)

为项目和loadimage处理程序添加一个通用的处理程序

class loadimage: ihttphandler
{
   public void Process(HttpContext context)
   {

        var id = context.Request["id"];
        var row = GetImageFromDb(id);

        var response = context.Response;
        response.AddHeader("content-disposition", "attachment; filename=" + row["anem of image"]);
        response.ContentType = row["mime type"].ToString(); //png or gif etc.
        response.BinaryWrite((byte[])row["image blob"]);
   }

   public bool Reuse { get {return true; } }
}
Run Code Online (Sandbox Code Playgroud)