MemoryStream を asp:image コントロールにバインドする方法はありますか?
8 に答える
最善の策は、画像を返す 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>
それだけです。
ハンドラーは、他のリクエストと同様に url パラメーターを受け入れることができます。したがって、にリンクする代わりに、に設定し<asp:image/>
ます。image.ashx
image.ashx?ImageID=[Your image ID here]
私はあなたがasp.netから動的な画像を生成する必要があると仮定しています .
Hanselman は最近それについてブログを書いてい ます http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx
@Will と Ben Griswald: "image.aspx" の代わりに "image.ashx" を使用します。
完全な ASP.Net ページよりも軽量で、text/html 以外のコンテンツ タイプを処理するように特別に設計されています。
TelerikのBinaryImageコントロールをASP.netに使用できます。
詳細はこちら: http ://www.telerik.com/products/aspnet-ajax/binaryimage.aspx
MemoryStream を Image にデータバインドすることはできませんが、Label/GenericControl、一部のコード、およびデータ URI スキームを使用して Image を Pages に埋め込むことは可能ですが、その方法には重大な問題があります。
短所
- 埋め込まれたコンテンツは、変更を行う前に抽出してデコードし、後で再エンコードして再度埋め込む必要があります。
- Cookie はサポートされていません。
- 複数回埋め込まれた情報は、含まれているファイルの一部として再ダウンロードされるため、ブラウザーのキャッシュの恩恵を受けません。
- ブラウザーは URI の長さを制限し、有効な最大データ サイズを作成する場合があります。たとえば、以前のバージョンの Opera の URI には 4kB の制限があり、IE8 Beta 1 では 32kB に制限されていました [要出典]
- データは単純なストリームとして含まれ、多くの処理環境 (Web ブラウザーなど) は、コンテナー (multipart/alternative または message/rfc822 など) を使用してメタデータ、データ圧縮、またはコンテンツ ネゴシエーションなどのより複雑なものを提供することをサポートしていない場合があります。
- Microsoft の Internet Explorer は、バージョン 7 (2008 年第 2 四半期の時点で市場の約 70%) までサポートされていません。
より良いアプローチは、別の「Image.aspx」ページを使用して、MemoryStream を取得および出力することです。これは、ASP.net の学習を開始したときに作成したフォト アルバム ソフトウェアで行ったようなものです。
(笑わないでください。ASP.net での最初の試みでした :-)
編集: ASHX に同意しました。上記のコードは、1 つのサンプル実装を示すためのものです。フォト アルバムを更新するときは、ASHX を使用します。
私にとっては、@Page に "buffer="false" を追加する必要がありました. そうしないと、常に同じ画像を取得し続けることになります...
いいえ。
ただし、その画像をストリーミングするための特別なページを作成できます。最初に、画像の 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();