0

Here is the code, the problem is that the image is displayed but after clearing all the page, i need it to be drawn inside the user control that is inside the web form.

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);

            }
        }
    }

I need to display the image where the user control is located inside the web form. Not in a new page.

I need to use a User Control it was specifically requested by my client.

Computer's Guy
  • 5,122
  • 8
  • 54
  • 74

1 Answers1

0

Adding

<img src="/imagehandler.ashx" />

and

public class ImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.OutputStream.Write(imageData, 0, imageData.Length);
        context.Response.ContentType = "image/JPEG";
    }
}

another solution to this problem is to put the image data in cache:

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 -->

Reference: bytearray to image asp.net

Community
  • 1
  • 1
Computer's Guy
  • 5,122
  • 8
  • 54
  • 74