.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Http Handlers for handling images on the fly

Posted By:Sunil Yadav       Posted Date: May 20, 2010    Points: 20    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article i am going to create an http Handler which is used to resize images on the fly.
 

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

 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend