C# 使用HTTP处理程序从数据库返回图像数组

C# 使用HTTP处理程序从数据库返回图像数组,c#,asp.net,http,handler,C#,Asp.net,Http,Handler,我编写了一个处理程序,从显示的数据库中返回一个图像。Nw我想要一个与特定图像相关的图像数组。例如,如果图像“A”与图像“B”、“C”和“D”相关,我希望http处理程序返回A、B、C和D图像。这样我就可以在网页上显示图像。如何返回图像数组或图像列表 这是我的处理程序代码 <%@ WebHandler Language="C#" Class="DisplayImg" %> using System; using System.Web; using System.Configurati

我编写了一个处理程序,从显示的数据库中返回一个图像。Nw我想要一个与特定图像相关的图像数组。例如,如果图像“A”与图像“B”、“C”和“D”相关,我希望http处理程序返回A、B、C和D图像。这样我就可以在网页上显示图像。如何返回图像数组或图像列表

这是我的处理程序代码

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

使用制度;
使用System.Web;
使用系统配置;
使用System.IO;
使用系统数据;
使用System.Data.SqlClient;
公共类显示img:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
串起;
if(context.Request.QueryString[“id”!=null)
theID=context.Request.QueryString[“id”].ToString();
其他的
抛出新ArgumentException(“未指定参数”);
context.Response.ContentType=“image/jpeg”;
流strm=显示图像(theID);
字节[]缓冲区=新字节[2048];
int byteSeq=strm.Read(缓冲区,0,2048);
while(byteSeq>0)
{
context.Response.OutputStream.Write(缓冲区,0,字节相等);
byteSeq=strm.Read(缓冲区,0,2048);
}
}
公共流显示图像(字符串ID)
{
SqlConnection connection=newsqlconnection(ConfigurationManager.ConnectionStrings[“SERVER”].ConnectionString.ToString());
string sql=“从tbl_ServerMaster中选择服务器\u映像\u图标,其中服务器\u代码=@ID”;
SqlCommand cmd=新的SqlCommand(sql,连接);
cmd.CommandType=CommandType.Text;
cmd.Parameters.AddWithValue(“@ID”,theID);
connection.Open();
object theImg=cmd.ExecuteScalar();
尝试
{
返回新的内存流((字节[])theImg);
}
抓住
{
返回null;
}
最后
{
connection.Close();
}
}
公共布尔可重用
{
得到
{
返回false;
}
}
} 

您无法在单个httphandler中执行此操作,因为您将返回一个字节流。以下是如何通过两个步骤执行此操作:

1) 编写一个新的httphandler,返回相关的图像URL列表

2) 使上述URL指向您的DisplayImg处理程序


浏览器将呈现第一个处理程序的结果,然后使用第二个处理程序(DisplayImg)获取每个图像.

我编辑了您的问题,因为您的capslock键似乎被卡住了。@Ankur:图像是如何相关的?@Razort4x我的图像可以是1对多相关的,也可以是1对1相关的。我将图像映射保存在DB中。因此,当选择特定图像时,我将在DB中检查与所选图像相关或映射的图像,并希望显示所有图像这些图片。非常感谢。您能详细说明一下解决方案吗?我对使用处理程序还不熟悉。@user1768031您找到过解决方案吗?