Tuesday, July 7, 2009

HTTP Handlers for Image resize in ASP.NET

Hello frnds,
Here I put the code of image resize using http handler.

Page:PhotoResizeHandler.ashx

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

using System;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
public class PhotoResizeHandler : IHttpHandler
{
string _StrImage = string.Empty;
string _StrExt = string.Empty;
int _IntThumbWidth;
int _IntThumbHeight;

public void ProcessRequest(HttpContext context)
{
if (context.Request.Params["Height"] != null)
{
try
{
_IntThumbHeight = int.Parse(context.Request.Params["Height"]);
}
catch
{
_IntThumbHeight = 0;
}
}
if (context.Request.Params["Width"] != null)
{
try
{
_IntThumbWidth = int.Parse(context.Request.Params["Width"]);
}
catch
{
_IntThumbWidth = 0;
}
}
if (context.Request.Params["Image"] != null)
{
try
{
_StrImage = (context.Request.Params["Image"]).ToString();
}
catch
{
_StrImage = string.Empty;
}
}
if (context.Request.Params["Ext"] != null)
{
try
{
_StrExt = (context.Request.Params["Ext"]).ToString();
}
catch
{
_StrExt = string.Empty;
}
}
ResizeImage(_StrImage, _IntThumbWidth, _IntThumbHeight, _StrExt);

}

public bool IsReusable
{
get
{
return false;
}
}


private void ResizeImage(string MainImage, int thumbWidth, int thumbHeight, string ext)
{
try
{
System.Drawing.Image Image = System.Drawing.Image.FromFile(MainImage);
int srcWidth = Image.Width;
int srcHeight = Image.Height;

int w = thumbWidth, h = thumbHeight;
#region "Proposnal Ratio"
// proposnal resize of your image
if (srcHeight > thumbHeight)
{
w = srcWidth * thumbHeight / srcHeight;
}
else
h = srcHeight;

if (srcWidth > thumbWidth)
{
h = srcHeight * thumbWidth / srcWidth;
}
else
if (w > srcWidth)
w = srcWidth;
if (w > thumbWidth)
h = srcHeight * thumbWidth / srcWidth;
if (h > thumbHeight)
w = srcWidth * thumbHeight / srcHeight;

thumbWidth = (thumbWidth > w ? w : thumbWidth);
thumbHeight = (thumbHeight > h ? h : thumbHeight);
thumbWidth = (thumbWidth == 0 ? 1 : thumbWidth);
thumbHeight = (thumbHeight == 0 ? 1 : thumbHeight);

#endregion

Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(Image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

switch (ext.ToUpper())
{
case ".JPG":
case ".JPEG":
HttpContext.Current.Response.ContentType = "image/Jpeg";
bmp.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".GIF":
HttpContext.Current.Response.ContentType = "image/Gif";
bmp.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".PNG":
System.IO.MemoryStream MemStream = new System.IO.MemoryStream();
HttpContext.Current.Response.ContentType = "image/Png";
bmp.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);

MemStream.WriteTo(HttpContext.Current.Response.OutputStream);
//bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png );
break;
}
Image.Dispose();
}
catch (Exception Exc)
{
throw Exc;
}

}
}

Now you need to add one aspx page and create one asp:image controls there.

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

Then in cs file with on page load event, try to put this line
img.ImageUrl = "PhotoResizeHandler.ashx?Image=" + MainImage + "&Width=65&Height=65&Ext=" + ext;

Hopes this will help you.

Regards,
Kinjal Shah

No comments: