C# 如何使用字节数组在Web窗体内的用户控件中显示图像?
这是代码,问题是显示图像,但在清除所有页面后,我需要在web表单中的用户控件中绘制图像C# 如何使用字节数组在Web窗体内的用户控件中显示图像?,c#,asp.net,image,user-controls,webforms,C#,Asp.net,Image,User Controls,Webforms,这是代码,问题是显示图像,但在清除所有页面后,我需要在web表单中的用户控件中绘制图像 protected void Page_Load(object sender, EventArgs e) { if (Session["ObjHoteles"] == null) { Label1.Text = "select hotel first please."; } else {
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ObjHoteles"] == null)
{
Label1.Text = "select hotel first please.";
}
else
{
if (!IsPostBack)
{
List<Byte[]> ArrayFotos = new List<Byte[]>();
string NombreDelHotel = "";
Hoteles Hotel1 = (Hoteles)Session["ObjHoteles"];
NombreDelHotel = Hotel1.NombreHotel;
ArrayFotos = Persistencia.PersistenciaFotos.FotosDeHotel(NombreDelHotel);
Session["CantFotos"] = ArrayFotos.Count();
Byte[] Foto = ArrayFotos[0];
Response.Buffer = true;
Response.ContentType = "image/jpeg";
Response.Expires = 0;
Response.OutputStream.Write(Foto, 0, Foto.Length);
Session["NumFoto"] = 0;
}
else
{
List<Byte[]> ArrayFotos = new List<Byte[]>();
string NombreDelHotel = "";
Hoteles Hotel1 = (Hoteles)Session["ObjHoteles"];
NombreDelHotel = Hotel1.NombreHotel;
ArrayFotos = Persistencia.PersistenciaFotos.FotosDeHotel(NombreDelHotel);
Session["CantFotos"] = ArrayFotos.Count();
Byte[] Foto = ArrayFotos[(int)Session["NumFoto"]];
Response.Buffer = true;
Response.Clear();
Response.ContentType = "image/jpeg";
Response.Expires = 0;
Response.BinaryWrite(Foto);
}
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(会话[“ObjHoteles”]==null)
{
Label1.Text=“请先选择酒店。”;
}
其他的
{
如果(!IsPostBack)
{
List ArrayFotos=新列表();
字符串NombreDelHotel=“”;
Hoteles Hotel1=(Hoteles)会话[“ObjHoteles”];
NombreDelHotel=Hotel1.NombreDelHotel;
ArrayFotos=Persistencia.PersistenciaFotos.FotosDeHotel(诺姆布雷德酒店);
会话[“CantFotos”]=ArrayFotos.Count();
字节[]Foto=ArrayFotos[0];
Response.Buffer=true;
Response.ContentType=“image/jpeg”;
Response.Expires=0;
Response.OutputStream.Write(Foto,0,Foto.Length);
会话[“NumFoto”]=0;
}
其他的
{
List ArrayFotos=新列表();
字符串NombreDelHotel=“”;
Hoteles Hotel1=(Hoteles)会话[“ObjHoteles”];
NombreDelHotel=Hotel1.NombreDelHotel;
ArrayFotos=Persistencia.PersistenciaFotos.FotosDeHotel(诺姆布雷德酒店);
会话[“CantFotos”]=ArrayFotos.Count();
字节[]Foto=ArrayFotos[(int)Session[“NumFoto”];
Response.Buffer=true;
Response.Clear();
Response.ContentType=“image/jpeg”;
Response.Expires=0;
响应。二进制写入(Foto);
}
}
}
我需要在web表单中显示用户控件所在的图像。不在新的页面中
我需要使用客户特别要求的用户控件。添加
<img src="/imagehandler.ashx" />
此问题的另一个解决方案是将图像数据放入缓存:
Guid id = Guid.NewGuid();
HttpRuntime.Cache.Add(id.ToString(), imageData);
And pass the key to the HttpHandler in the querystring, so it can fetch it from cache:
<img src="/imagehandler.ashx?img=<%=id%>" />
<!-- will print ...ashx?img=42a96c06-c5dd-488c-906f-cf20663d0a43 -->
Guid id=Guid.NewGuid();
HttpRuntime.Cache.Add(id.ToString(),imageData);
并将密钥传递给querystring中的HttpHandler,以便它可以从缓存中获取密钥:
" />
参考资料:试试这个,谢谢迈克尔B。太好了,别忘了给我一些爱
Guid id = Guid.NewGuid();
HttpRuntime.Cache.Add(id.ToString(), imageData);
And pass the key to the HttpHandler in the querystring, so it can fetch it from cache:
<img src="/imagehandler.ashx?img=<%=id%>" />
<!-- will print ...ashx?img=42a96c06-c5dd-488c-906f-cf20663d0a43 -->