如何在MemoryStream中显示图像



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

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

是,

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

html

<img src="loadimage.ashx?id=..."/>

将通用处理程序添加到项目和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; } }
}

我正在使用以下代码将图像从远程URL获取到-MemoryStream-System.Drawing.Image

// Set image url
string imgURL = ("http://cdn.flikr.com/images/products/" + imageName);
// Get image into memory
WebClient lWebClient = new WebClient();
byte[] lImageBytes = lWebClient.DownloadData(imgURL);
MemoryStream imgStream = new MemoryStream(lImageBytes);
image = System.Drawing.Image.FromStream(imgStream);
if (image.Width >= 200)
{
    // do something
}

最新更新