14

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

4

8 回答 8

40

最好的办法是创建一个返回图像的 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>

就是这样。

于 2008-09-05T20:12:12.867 回答
7

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

于 2008-09-05T21:10:42.633 回答
4

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

Hanselman 最近写了一篇关于它的博客 http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx

于 2008-09-05T20:13:19.027 回答
2

@Will 和 Ben Griswald:使用“image.ashx”代替“image.aspx”。

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

于 2008-09-05T20:18:03.523 回答
1

虽然无法将 MemoryStream 数据绑定到图像,但可以使用标签/通用控件、一些代码和数据 URI 方案将图像嵌入页面中,但这种方法存在严重问题:

缺点

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

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

(别笑,那是我第一次尝试 ASP.net :-)

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

于 2008-09-05T20:16:29.033 回答
1

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

更多信息在这里: http ://www.telerik.com/products/aspnet-ajax/binaryimage.aspx

于 2012-11-28T14:09:13.370 回答
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();
于 2008-09-05T20:22:14.527 回答
0

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

于 2013-01-14T04:30:15.587 回答