如何将MemoryStream绑定到asp:image控件?

时间:2008-09-05 20:05:53

标签: asp.net image memorystream

有没有办法将MemoryStream绑定到asp:image control?

8 个答案:

答案 0 :(得分:39)

最好的办法是创建一个可以返回图像的HttpHandler。然后将asp:Image上的ImageUrl属性绑定到HttpHandler的url。

这是一些代码。

首先创建HttpHandler:

<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

public class ImageHandler : IHttpHandler
{    
    public void ProcessRequest (HttpContext context)
    {
        context.Response.Clear();

        if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
        {
            int id = Int32.Parse(context.Request.QueryString["id"]);

            // Now you have the id, do what you want with it, to get the right image
            // More than likely, just pass it to the method, that builds the image
            Image image = GetImage(id);

            // Of course set this to whatever your format is of the image
            context.Response.ContentType = "image/jpeg";
            // Save the image to the OutputStream
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        }
        else
        {
            context.Response.ContentType = "text/html";
            context.Response.Write("<p>Need a valid id</p>");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
   }

   private Image GetImage(int id)
   {
       // Not sure how you are building your MemoryStream
       // Once you have it, you just use the Image class to 
       // create the image from the stream.
       MemoryStream stream = new MemoryStream();
       return Image.FromStream(stream);
   }
}

接下来,只需在您使用asp:Image。

的aspx页面中调用它
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Image ID="myImage" ImageUrl="~/ImageHandler.ashx?id=1" runat="server" />
        </div>
    </form>
</body>
</html>

就是这样。

答案 1 :(得分:7)

处理程序可以像任何其他请求一样接受url参数。因此,不要将<asp:image/>image.ashx关联起来,而是将其设置为image.ashx?ImageID=[Your image ID here]

答案 2 :(得分:4)

我假设您需要从asp.net生成动态图像 你可能很幸运 http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449

Hanselman最近在博客上发表了这篇文章 http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx

答案 3 :(得分:2)

@Will和Ben Griswald:而不是“image.aspx”使用“image.ashx”。

它比完整的ASP.Net页面更轻量级,它专门用于处理text / html以外的内容类型。

答案 4 :(得分:1)

虽然无法将MemoryStream数据绑定到图像,但可以使用Label / GenericControl,某些代码和data URI scheme在页面中嵌入图像,但这种方法存在严重问题: / p>

  

缺点

     
      
  • 必须先提取并解码嵌入内容,然后才能进行更改,然后重新编码并重新嵌入。
  •   
  • 不支持Cookie。
  •   
  • 多次嵌入的信息将作为包含文件的一部分重新加载,因此无法从浏览器的缓存中受益。
  •   
  • 浏览器可能会限制URI长度,从而创建有效的最大数据大小。例如,以前版本Opera中的URI限制为4kB,IE8 Beta 1限制为32kB [需要引证]
  •   
  • 数据作为简单流包含在内,许多处理环境(如Web浏览器)可能不支持使用容器(例如multipart / alternative或message / rfc822)来提供更高的复杂性,例如元数据,数据压缩或内容协商。
  •   
  • 微软的Internet Explorer,通过版本7(截至2008年第二季度约70%的市场份额),缺乏支持。
  •   

更好的方法是使用一个单独的“Image.aspx”页面来获取和输出你的MemoryStream,有点像我在我开始学习ASP.net时创建的Photo Album软件中所做的那样:

(Don't laugh, that was my first attempt at ASP.net :-)

编辑:在ASHX上达成一致,上面的代码只是为了展示一个示例实现。当我来更新相册时,它将使用ASHX。

答案 5 :(得分:1)

您可以对ASP.net使用Telerik的BinaryImage控件。

此处有更多信息: http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx

答案 6 :(得分:0)

不。

但您可以创建一个特殊页面来流式传输该图像。首先,您将图像的URL设置为执行流式处理的页面,包括一些url参数,让您知道从何处获取图像:

<img src="GetImage.aspx?filename=foo" ... />

在GetImage.aspx中,您从URL获取文件名(或其他),在MemoryStream中加载图像,然后将该内存流的内容直接写入HttpResponse:

    response.Expires = 0;
    response.Buffer = false;
    response.Clear();
    response.ClearHeaders();
    response.ClearContent();
    response.ContentType = "image/jpeg";
    response.BinaryWrite(stream);
    response.Flush();
    response.Close();

答案 7 :(得分:0)

对我来说,有必要在@Page中添加“buffer =”false“。否则我会一直得到同样的图片......

相关问题