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

发布于 2024-07-04 10:28:46 字数 44 浏览 7 评论 0原文

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

Is there a way to bind a MemoryStream to asp:image control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怕倦 2024-07-11 10:28:46

没有。

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

Nope.

But you can create a special page to stream that image out. First, you set the URL of the image to the page that performs the streaming, including some url parameters that let you know where to get the image:

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

in GetImage.aspx, you get the filename (or whatever) from the URL, load the image in your MemoryStream, and then write the content of that memory stream directly to the HttpResponse:

    response.Expires = 0;
    response.Buffer = false;
    response.Clear();
    response.ClearHeaders();
    response.ClearContent();
    response.ContentType = "image/jpeg";
    response.BinaryWrite(stream);
    response.Flush();
    response.Close();
如果没有 2024-07-11 10:28:46

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

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

You can use Telerik's BinaryImage control for ASP.net.

More info in here:
http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx

笑梦风尘 2024-07-11 10:28:46

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

For me it was necessary to add "buffer="false" to the @Page. Otherwise I would keep getting the same picture all the time...

゛清羽墨安 2024-07-11 10:28:46

处理程序可以像任何其他请求一样接受 url 参数。 因此,您不必将 链接到 image.ashx,而是将其设置为 image.ashx?ImageID=[您的图像 ID在这里]

A handler can accept a url parameter like any other request. So instead of linking your <asp:image/> to image.ashx you'd set it to image.ashx?ImageID=[Your image ID here].

请持续率性 2024-07-11 10:28:46

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

Hanselman 最近在博客上介绍了它

I am assuming you need to generate dynamic images from asp.net
You might be in luck
http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449

Hanselman blogged about it recently
http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx

旧人 2024-07-11 10:28:46

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

缺点

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

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

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

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

While Databinding a MemoryStream to a Image is not possible, it could be possible to use a Label/GenericControl, some Code and the data URI scheme to embed Images in Pages, but there are severe issues with that approach:

Disadvantages

  • Embedded content must be extracted and decoded before changes may be made, then re-encoded and re-embedded afterwards.
  • Cookies are not supported.
  • Information that is embedded more than once is redownloaded as part of the containing file, and thus does not benefit from the browser's cache.
  • Browsers may limit URI lengths, creating an effective maximum data size. For example, URIs in previous versions of Opera had limits of 4kB, and 32kB for IE8 Beta 1[citation needed]
  • Data is included as a simple stream, and many processing environments (such as web browsers) may not support using containers (such as multipart/alternative or message/rfc822) to provide greater complexity such as metadata, data compression, or content negotiation.
  • Microsoft's Internet Explorer, through version 7 (some 70% of the market as of 2008 Q2), lacks support.

The better Approach is to use a separate "Image.aspx" Page which takes and outputs your MemoryStream, kinda like I did in my Photo Album software that i've created when I started learning ASP.net:

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

Edit: Agreed on ASHX, the code above is just to show one sample implementation. When I come around to update the Photo Album, it will use ASHX for that.

老旧海报 2024-07-11 10:28:46

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

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

@Will and Ben Griswald: instead of "image.aspx" use "image.ashx".

It's more light-weight than a full ASP.Net Page, and it's specifically designed to handle content-types other than text/html.

恋竹姑娘 2024-07-11 10:28:46

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

就是这样。

Best bet is to create an HttpHandler that would return the image. Then bind the ImageUrl property on the asp:Image to the url of the HttpHandler.

Here is some code.

First create the 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);
   }
}

Next, just call it inside your aspx page where you are using the asp:Image.

<%@ 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>

And that is it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文