Making thumbnail dynamically with ashx file in asp.net

In this article I am going to show how to resize image dynamically or making thumbnail dynamically with ashx file.Start new website project give name thumbnail.In menu go to Webite > Add new item and select Generic Handler. Name it Handler.ashx, You will get auto generated code in it like given below:

 

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

 


Now add given namespace to handle the image files

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;


Make a new folder for images files, name it images. Put some images in this folder.

Now our next step is getting the height , width , image name and changing image size.
 To do these things, here query string is used. In ProcessRequest method write follwing lines.

 

 

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
 // for new height of image
  int h=int.Parse(context.Request.QueryString["h"].ToString()); 
 // for new width of image
        int w = int.Parse(context.Request.QueryString["w"].ToString());
         // for  image file name
        string file = context.Request.QueryString["file"].ToString();

 // Path of image folder where images files are placed
 string  filePath = context.Server.MapPath("~/images/" + file);

 // Resize proccess
   using(System.Drawing.Image img=System.Drawing.Image.FromFile(filePath))
    {
       Bitmap objBmp = new Bitmap(img,w, h);
        string extension = Path.GetExtension(filePath);
        MemoryStream ms; 
        byte[] bmpBytes;
        switch (extension.ToLower())
        {
            case ".jpg":
            case ".jpeg": 
                ms = new MemoryStream();
                objBmp.Save(ms, ImageFormat.Jpeg);
                bmpBytes = ms.GetBuffer();
                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(bmpBytes);
                objBmp.Dispose();
                ms.Close();
                context.Response.End();
                break;
            case ".png":
                  ms = new MemoryStream();
                  objBmp.Save(ms, ImageFormat.Png);
                  bmpBytes = ms.GetBuffer();
                 context.Response.ContentType = "image/png";
                 context.Response.BinaryWrite(bmpBytes);
                 objBmp.Dispose();
                 ms.Close();
                 context.Response.End();
            break;
           case ".gif":
                ms = new MemoryStream();
                objBmp.Save(ms, ImageFormat.Gif);
                bmpBytes = ms.GetBuffer();
                context.Response.ContentType = "image/png";
                context.Response.BinaryWrite(bmpBytes);
                objBmp.Dispose();
                ms.Close();
                context.Response.End();
                break;
  
        }
        img.Dispose();
   }

    }

 

 

Now we move to Default.aspx page, drag here  Image tool from tool box :

 <asp:Image ID="img1" runat="server" />

For retriving the image set ImageUrl as given below with height width and image name.

<asp:Image ID="img1" runat="server" ImageUrl="~/Handler.ashx?h=50&w=50&file=Winter.jpg" />

And now finally run the default.aspx and see the result.