Wednesday, September 19, 2012

Accessing session Value in Http Handlers and from Http Handlers

In this post i will explain how to access session Value in Http Handlers and from session value from Http Handlers.

In the previous post Dynamic Thumbnail Images using Http Handlers in asp.net C# explained how to show images using Http Handlers.

To access session value in Http Handler use following code

Ex :

In the code behind


         Session["imgID"] = "2";



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

using System;
using System.Web;
using System.Drawing;
using System.IO;

public class ThumbNail : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
    public void ProcessRequest(HttpContext context)
    {      
        string imageurl = context.Session["imgID"].ToString() + ".jpg";
        string str = context.Server.MapPath("~/images/" + imageurl);
        Bitmap bmp = new Bitmap(str);
        System.Drawing.Image img = bmp.GetThumbnailImage(250, 250, nullIntPtr.Zero);
        MemoryStream ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] bmpBytes = ms.GetBuffer();
        img.Dispose();
        ms.Close();

        context.Response.BinaryWrite(bmpBytes);
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

No comments:

Post a Comment