Monday, 22 June 2015

Image Preview in ASP.NET


//.aspx.cs
    protected void btnPreview_Click(object sender, EventArgs e)
    {

        if (fuplCandidatePhoto.HasFile)
        {
            ImagePreview.Visible = true;
            Session["ImageBytes"] = fuplCandidatePhoto.FileBytes;
            ImagePreview.ImageUrl = "~/ImageHandler.ashx";
        }
  }


//imagehandler.ashx


<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;

public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
   
    public void ProcessRequest (HttpContext context)
    {
        //Checking whether the imagebytes session variable have anything else not doing anything

        if ((context.Session["ImageBytes"]) != null)
        {
            byte[] image = (byte[])(context.Session["ImageBytes"]);
            context.Response.ContentType = "image/JPEG";
            context.Response.BinaryWrite(image);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

No comments:

Post a Comment