2012-01-11 87 views
1

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

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

+0

相信这是http://stackoverflow.com/questions/46788/how-to-bind-a-memorystream-to-aspimage-control – Jagd 2012-01-11 16:38:45

回答

10

是,

  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; } } 
} 
+0

复制感谢您的帮助! – Jagd 2012-01-11 16:41:15

1

我使用下面的代码从远程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 
}