ASPページベースの描画チュートリアルでは、内部のHttpHandlerの代わりに、カスタムASPページが使用されているので、グラフの描画方法を高度に制御できます。このメソッドを使用することにより、クライアントキャッシュ、サーバーキャッシュ、IIS のロギングを含む応答の管理をカスタマイズできます。
以下のコード例は、ASP ページの ImageRenderMethod プロパティを使用します。両方とも HTTPHandler に非常に類似しています。ただし、HTTPHandler エントリを web.config に追加する代わりに、指定した ASP ページを作成および追加して <IMG> タグのソース属性の URL 要求を処理する必要があります。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Imports System
Imports System.Web
Imports System.Web.SessionState
Namespace AspPageSample
_
Public Class StreamResponse
Inherits System.Web.UI.Page
' イメージをこのページに転送する方法を決定します。
Private Sub Page_Load(sender As Object, e As System.EventArgs)
Dim ses As String = Request("SessionID") ' セッション転送の場合は非 Null 値。
Dim cac As String = Request("CacheID") ' キャッシュの転送の場合は非 Null 値。
Dim del As String = Request("Delete") ' 削除要求の場合は非 Null 値。
Dim doDelete As Boolean = False
' 削除要求ステータスをチェックします。
If Not (del Is Nothing) Then
del = del.ToLower()
doDelete = del = "t" Or del = "true"
End If
Dim image As Byte() = Nothing
Dim imgtype As String = String.Empty
Dim img As String = String.Empty
Dim hsh As String = String.Empty
If Not (ses Is Nothing) Then
' セッションオブジェクトからイメージ情報を抽出します。
img = ses + "_ImageBytes"
hsh = ses + "_Hash"
image = CType(Session(img), Byte())
imgtype = CStr(Session(hsh))
If doDelete Then
Session.Remove(img)
Session.Remove(hsh)
End If
Else
If Not (cac Is Nothing) Then
' アプリケーションキャッシュからイメージ情報を抽出します。
img = cac + "_ImageBytes"
hsh = cac + "_Hash"
image = CType(Cache(img), Byte())
imgtype = CStr(Cache(hsh))
If doDelete Then
Cache.Remove(img)
Cache.Remove(hsh)
End If
End If
End If
If Not (image Is Nothing) And Not (imgtype Is Nothing) Then
' バイナリイメージをクライアントに返します。
imgtype = "image/" + imgtype.Substring((imgtype.LastIndexOf("."c) + 1))
Response.Clear()
Response.Cache.SetExpires(DateTime.MinValue) ' クライアントキャッシュを消去します。
Response.ContentType = imgtype
Response.BinaryWrite(image)
Response.Flush()
End If
End Sub 'Page_Load
End Class 'StreamResponse
End Namespace 'AspPageSample
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
using System;
using System.Web;
using System.Web.SessionState;
namespace AspPageSample
{
public class StreamResponse : System.Web.UI.Page
{
// イメージをこのページに転送する方法を決定します。
private void Page_Load(object sender, System.EventArgs e)
{
string ses = Request["SessionID"]; // セッション転送の場合は非 Null 値。
string cac = Request["CacheID"]; // キャッシュの転送の場合は非 Null 値。
string del = Request["Delete"]; // 削除要求の場合は非 Null 値。
bool doDelete = false;
// 削除要求ステータスをチェックします。
if(del != null)
{
del = del.ToLower();
doDelete = (del == "t" |
}
byte [] image = null;
string imgtype = string.Empty;
string img = string.Empty;
string hsh = string.Empty;
if(ses != null)
{
// セッションオブジェクトからイメージ情報を抽出します。
img = ses + "_ImageBytes";
hsh = ses + "_Hash";
image = (byte[])Session[img];
imgtype = (string)Session[hsh];
if(doDelete)
{
Session.Remove(img);
Session.Remove(hsh);
}
}
else if(cac != null)
{
// アプリケーションキャッシュからイメージ情報を抽出します。
img = cac + "_ImageBytes";
hsh = cac + "_Hash";
image = (byte[])Cache[img];
imgtype = (string)Cache[hsh];
if(doDelete)
{
Cache.Remove(img);
Cache.Remove(hsh);
}
}
if(image != null && imgtype != null)
{
// バイナリイメージをクライアントに返します。
imgtype = "image/" + imgtype.Substring(imgtype.LastIndexOf('.')+1);
Response.Clear();
Response.Cache.SetExpires(DateTime.MinValue); // クライアントキャッシュを消去します。
Response.ContentType = imgtype;
Response.BinaryWrite(image);
Response.Flush();
}
}
}
}
|
|