1

表示されるデータベースから 1 つの画像を返すハンドラーを作成しました。特定の画像に関連する画像の配列が必要です。のように、画像 " A が画像 "B"、"C" および "D" に関連している場合、A、B、C、および D の画像が http ハンドラーによって返されるようにします。Web 上で画像を表示できるようにします。画像の配列または画像のリストを返すにはどうすればよいですか?

ここに私のハンドラコードがあります。

<%@ WebHandler Language="C#" Class="DisplayImg" %>

using System;
using System.Web;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class DisplayImg : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string theID;
        if (context.Request.QueryString["id"] != null)
            theID = context.Request.QueryString["id"].ToString();
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg";
        Stream strm = DisplayImage(theID);
        byte[] buffer = new byte[2048];
        int byteSeq = strm.Read(buffer, 0, 2048);

        while (byteSeq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq = strm.Read(buffer, 0, 2048);
        }
    }

    public Stream DisplayImage(string theID)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString.ToString());
        string sql = "SELECT Server_image_icon FROM tbl_ServerMaster WHERE server_Code = @ID";
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", theID);
        connection.Open();
        object theImg = cmd.ExecuteScalar();
        try
        {
            return new MemoryStream((byte[])theImg);
        }
        catch
        {
            return null;
        }
        finally
        {
            connection.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
} 
4

1 に答える 1

0

バイトのストリームを返すため、単一のhttphandlerでそれを行うことはできません。これを2つのステップで行う方法は次のとおりです。

1)関連する画像URLのリストを返す新しいhttphandlerを記述します。

2)上記のURLがDisplayImgハンドラーを指すようにします。

ブラウザは最初のハンドラーの結果をレンダリングし、次に2番目のハンドラー(DisplayImg)を使用して各画像をフェッチします。

于 2012-11-08T05:11:43.063 に答える