Click here to Skip to main content
15,881,380 members
Articles / Resize
Article

Http Handlers to handle Images

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 7.2K   1  
Hi,In this article i am going to create an http Handler which is used to resize my images and display it  .What are HTTP Handlers?HTTP handlers are

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Hi,

In this article i am going to create an http Handler which is used to resize my images and display it  .

What are HTTP Handlers?

HTTP handlers are the .NET components that implement the System.Web.IHttpHandler interface, they can act as a target for the incoming HTTP requests and can be called directly by using their file name in the URL.
HTTP handlers implement the following two methods:
1) ProcessRequest: called to process http requests and
2) IsReusable which indicates whether this instance of http handler can be reused for fulfilling another requests of the same type.

In your website Add new Item Generic Handler and name it as ImageHandler.ashx

Here is ImageHandler.ashx file


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

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

public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string sImageFileName = "";
        int iThumbSize = 0;
        decimal dHeight, dWidth, dNewHeight, dNewWidth;

        sImageFileName = context.Request.QueryString["img"];
        iThumbSize = Convert.ToInt32(context.Request.QueryString["sz"]);
       
        System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(System.Web.HttpContext.Current.Server.MapPath("Image Path" + sImageFileName));
        if (sImageFileName != null)
        {
            if (iThumbSize == 1)
            {

                dHeight = objImage.Height;
                dWidth = objImage.Width;
                dNewHeight = 120;
                dNewWidth = dWidth * (dNewHeight / dHeight);
                objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new System.Drawing.Image.GetThumbnailImageAbort(callback), new IntPtr());  
            }
            if (iThumbSize == 2)
            {
                
                dHeight = objImage.Height;
                dWidth = objImage.Width;
                dNewHeight = 200;
                dNewWidth = dWidth * (dNewHeight / dHeight);
                objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new System.Drawing.Image.GetThumbnailImageAbort(callback), new IntPtr());
            }
           
            MemoryStream objMemoryStream = new MemoryStream();
            objImage.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imageContent = new byte[objMemoryStream.Length];
            objMemoryStream.Position = 0;
            objMemoryStream.Read(imageContent, 0, (int)objMemoryStream.Length);
            context.Response.ContentType = "image/jpeg";
            context.Response.BinaryWrite(imageContent);
        }
    }


    private bool callback()
    {
        return true;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

Following is the way to write it..

 <img id="Image1"  src='../ImageHandler.ashx?img=ImageName&sz=1'  />

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --