Introduction:
Have you ever thought of streaming thumbnails just by passing query string indicating width or height of thumbnail you need, and most importantly passing those to image itself?
<img src=MyImage.jpg?Height=200>
Well if you have little knowledge of what HTTP Handlers are, you can do that easily. So what are HTTP handlers?
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.
HTTP handlers can return either true or false in order to specify whether they can be reused. So lets Create Handler for Images.
Following HttpImageHandler Class Implements IHttpHandler Interface.
using
System;using System.Web;
using System.IO ;
using System.Drawing;
using System.Drawing.Imaging;
namespace ImageHandler
{
public class HttpImageHandler: IHttpHandler
{
int Width = 0;
int Height = 0;
public void ProcessRequest(System.Web.HttpContext context)
{
if(context.Request.Params["height"] != null)
{
try
{
Height = int.Parse(context.Request.Params["height"]);
}
catch
{
Height = 0;
}
}
if(context.Request.Params["width"] != null)
{
try
{
Width = int.Parse(context.Request.Params["width"]);
}
catch
{
Width = 0;
}
}
if (Width <= 0 && Height <=0)
{
context.Response.Clear();
context.Response.ContentType = getContentType(context.Request.PhysicalPath); context.Response.WriteFile(context.Request.PhysicalPath);
context.Response.End();
}
else
{
context.Response.Clear();
context.Response.ContentType = getContentType(context.Request.PhysicalPath);
byte[] buffer = getResizedImage(context.Request.PhysicalPath,Width,Height);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
byte[] getResizedImage(String path ,int width,int height)
{
Bitmap imgIn = new Bitmap(path);
double y = imgIn.Height;
double x = imgIn.Width;
double factor = 1;
if(width > 0)
{
factor = width/x;
}
else if (height>0)
{
factor = height/y;
}
System.IO.MemoryStream outStream = new System.IO.MemoryStream();
Bitmap imgOut = new Bitmap((int)(x * factor),(int)(y * factor));
Graphics g = Graphics.FromImage(imgOut);
g.Clear(Color.White);
g.DrawImage(imgIn,new Rectangle(0,0,(int)(factor * x),(int)(factor * y)),new Rectangle(0,0,(int)x,(int)y),GraphicsUnit.Pixel);
imgOut.Save(outStream, getImageFormat(path));
return outStream.ToArray();
}
string getContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default : break;
}
return "";
}
ImageFormat getImageFormat(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default : break;
}
return ImageFormat.Jpeg;
}
}
}



Chandu KumawatPosted Dec 10, 2015, 7:38 AM
good
Jo KendalPosted Aug 17, 2010, 3:54 PM
Hi. Great article. I tried to follow it through in a Windows 7 environment and got a little lost with the very different interfaces for handlers etc. Could you extend the tutorial to cover the new properties interface in Windows 7
Rakesh YadavPosted May 24, 2007, 11:42 PM
hello sir.. this artical is excellent...but how can i use this for http://www.abc.com/image.jpg when path of image is like this.. then image handler is not working. please help me.