如何从一个图像处理程序中检索多个图像?

时间:2014-10-06 15:55:02

标签: c# html asp.net

我已经看到了一些类似于我的其他问题,但我还没有看到如何使用它来修复我的代码。我有一个带有下拉列表的webform,用于选择你想要的图片。我的问题是,现在我为每个图像使用多个图像处理程序。以下是代码示例:

网络形式:

namespace MultiCameraPage
{
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void SelBut_Click(object sender, EventArgs e)
    {
        int num = DropDownList1.SelectedIndex;


        switch (num)
        {
            case 0:
                Response.Redirect("Page1.htm");
                break;
            case 1:
                Response.Redirect("Page2.htm");
                break;
            case 2:
                Response.Redirect("Page3.htm");
                break;
            case 3:
                Response.Redirect("Page4.htm");
                break;

        }

    }

}
}

它只是重定向到包含您想要的图片的网页。我将展示" page1"和"第2页"其余的只是改变名字的说法。

page1 html:

<head>
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript" language="JavaScript">
    function refreshIt() {
        if (!document.images) return;
        document.getElementById("imgcontainer1").src = "/Page1Handler.ashx?" + Math.random();
        setTimeout('refreshIt()', 700);
    }
</script>
</head>
<body onload=" setTimeout('refreshIt()',700)">
<img id="imgcontainer1" src="/Page1Handler.ashx" alt="cam image1"/>
</body>
</html>

Page1Handler:

此处理程序必须从文件流中保存图片

namespace MultiCameraPage
{

public class Page1Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string saveTo = @"C:pathtoImage\images\XIG.jpg";
        FileStream writeStream1 = new FileStream(saveTo, FileMode.OpenOrCreate, FileAccess.ReadWrite);

        using (FileStream fs1 = File.Open(@"C:\Path to filestream", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            ReadWriteStream1(fs1, writeStream1);
        }
        byte[] tt = File.ReadAllBytes(context.Server.MapPath("~/images/XIG.jpg"));
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(tt);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    // readStream is the stream you need to read
    // writeStream is the stream you want to write to
    private void ReadWriteStream1(Stream readStream, Stream writeStream)
    {
        int Length = 256;
        Byte[] buffer = new Byte[Length];
        int bytesRead = readStream.Read(buffer, 0, Length);
        // write the required bytes
        while (bytesRead > 0)
        {
            writeStream.Write(buffer, 0, bytesRead);
            bytesRead = readStream.Read(buffer, 0, Length);
        }
        readStream.Close();
        writeStream.Close();
    }
}
}

Page2 html:

它与page1相同,只是名称已更改

<head>
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript" language="JavaScript">
    function refreshIt() {
        if (!document.images) return;
        document.getElementById("imgcontainer2").src = "/Page2.ashx?" + Math.random();
        setTimeout('refreshIt()', 700);
    }
</script>
</head>
<body onload=" setTimeout('refreshIt()',700)">
<img id="imgcontainer2" src="/Page2.ashx" alt="cam image2"/>
</body>

Page2.ashx:

与page1Handler.ashx相同

namespace MultiCameraPage
{

public class Page2 : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string saveTo = @"C:\PathToImage\images\GateV.jpg";
        FileStream writeStream3 = new FileStream(saveTo, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

        using (FileStream fs3 = File.Open(@"C:\path to filestream", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            ReadWriteStream3(fs3, writeStream3);
        }
        byte[] tt = File.ReadAllBytes(context.Server.MapPath("~/images/GateV.jpg"));
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(tt);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    // readStream is the stream you need to read
    // writeStream is the stream you want to write to
    private void ReadWriteStream3(Stream readStream, Stream writeStream)
    {
        int Length = 256;
        Byte[] buffer = new Byte[Length];
        int bytesRead = readStream.Read(buffer, 0, Length);
        // write the required bytes
        while (bytesRead > 0)
        {
            writeStream.Write(buffer, 0, bytesRead);
            bytesRead = readStream.Read(buffer, 0, Length);
        }
        readStream.Close();
        writeStream.Close();
    }
}
}

其余的只是重复这些文件。 我觉得这是最糟糕的做法,这就是我寻求帮助的原因。下拉菜单附加到仅具有图片名称的数据表,而不包含任何其他内容。我应该在数据表中添加其他内容吗?

1 个答案:

答案 0 :(得分:1)

你绝对应该能够使用一个处理程序并将图像名称作为参数传递给它。所以你的图像标签看起来像:

<img id="imgcontainer2" src="/ImageHandler.ashx?imageName=GateV.jpg" alt="cam image2"/>

处理程序本身只决定加载读取此参数的图像:

public void ProcessRequest(HttpContext context)
{
    string imageName = context.Request["imageName"]; //make sure to handle case when this param is missing

    string saveTo = string.Format(@"C:pathtoImage\images\{0}", imageName);
    ...