首页 / 知识

关于asp.net:如何将MemoryStream绑定到asp:图像控件?

2023-04-14 09:15:00

关于asp.net:如何将MemoryStream绑定到asp:图像控件?

How to bind a MemoryStream to asp:image control?

是否可以将MemoryStream绑定到asp:image控件?


最好的选择是创建一个将返回图像的HttpHandler。然后将asp:Image上的ImageUrl属性绑定到HttpHandler的URL。

这是一些代码。

首先创建HttpHandler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<%@ 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页面内调用它即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ 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">
   
</head>
<body>
    <form id="form1" runat="server">
       
           
       
    </form>
</body>
</html>

就是这样。


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


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

Hanselman最近在此发表了博客
http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx


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

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


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

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


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

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.

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

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

编辑:同意ASHX,上面的代码仅用于显示一个示例实现。当我来更新相册时,它将使用ASHX。


对我来说,有必要在@Page上添加" buffer =" false",否则我将一直获取相同的图片...


不。

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

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

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

1
2
3
4
5
6
7
8
9
    response.Expires = 0;
    response.Buffer = false;
    response.Clear();
    response.ClearHeaders();
    response.ClearContent();
    response.ContentType ="image/jpeg";
    response.BinaryWrite(stream);
    response.Flush();
    response.Close();

绑定控件图像选择

最新内容

相关内容

猜你喜欢