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

有帮助吗?

解决方案

处理程序可以像任何其他请求一样接受 url 参数。所以而不是链接你的 <asp:image/>image.ashx 你会把它设置为 image.ashx?ImageID=[Your image ID here].

其他提示

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

就是这样。

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

汉塞尔曼最近在博客上谈到了这一点http://www.hanselman.com/blog/ASPNETFuturesGenerateDynamicImagesWithHttpHandlersGetsEasier.aspx

@威尔和本·格里斯瓦尔德:使用“image.ashx”代替“image.aspx”。

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

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

缺点

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

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

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

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

您可以将 Telerik 的 BinaryImage 控件用于 ASP.net。

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

没有。

但您可以创建一个特殊页面来流式传输该图像。首先,将图像的 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();

对我来说,有必要将“buffer=”false”添加到@Page。不然我会一直看到同一张照片……

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top