如何从一个图像处理程序中检索多个图像
本文关键字:程序 检索 图像 图像处理 一个 | 更新日期: 2023-09-27 18:25:51
我看到了一些其他类似的问题,但我不太明白如何使用这些问题来修复我的代码。我有一个带有下拉列表的网络表单,用于选择您想要的图片。我的问题是,现在我正在为每个图像使用多个图像处理程序。以下是代码示例:
网络表单:
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;
}
}
}
}
它只是重定向到有你想要的图片的网页。我会显示"第1页"answers"第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>
Page1处理程序:
此处理程序必须保存来自文件流的图片
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:
它与第1页相同,只是名称更改了
<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();
}
}
}
剩下的只是这些文件的重复。我觉得这是最糟糕的方式,这就是我寻求帮助的原因。下拉菜单附加到一个数据表,该数据表只有图片的名称,没有其他内容。我应该在数据表中添加其他内容吗?
您肯定应该能够使用一个处理程序并将图像名称作为参数传递给它
<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);
...