Updated on Kisan Patel
Here you will learn how to create and load thumbnail from original image using Generic handler in ASP.Net.
In this demo, we have create handler to load thumbnail from original size.
To create Generic Handler in ASP.NET, Right Click on the root project folder » Add » New Item » Choose Generic Handler from the list.
thumbnailIHandler.ashx (Generic Handler)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.IO; using System.Web.Services; namespace ImageThumbnailDemo { /// <summary> /// Summary description for thumbnailIHandler /// </summary> public class thumbnailIHandler : IHttpHandler { string strFilePath; public void ProcessRequest(HttpContext context) { try { { string strPath = context.Request.QueryString["P"]; string[] strData = null; strData = context.Request.QueryString["D"].ToLower().Split('x'); string strWidth = strData[0]; string strHeight = strData[1]; string intResizeType = "1"; if (strData.Length > 2) { intResizeType = strData[2]; } strFilePath = context.Server.MapPath("~\\" + strPath); context.Response.Clear(); context.Response.ContentType = "image/jpeg"; ResizeImageFile(strFilePath, Convert.ToInt32(strHeight), Convert.ToInt32(strWidth), Convert.ToSingle(intResizeType)).WriteTo(context.Response.OutputStream); context.Response.End(); } } catch (Exception ex) { } } public bool IsReusable { get { return false; } } private MemoryStream ResizeImageFile(string strFileName, int height, int width, float intResizeType) { using (Image oldImage = System.Drawing.Image.FromFile(strFileName)) { int oldwidth = oldImage.Width; int oldheight = oldImage.Height; int thumbHeight = height; int thumbWidth = width; if ((intResizeType == 1)) { if ((oldwidth > width)) { double def = width; double imgwidth = Convert.ToDouble(oldwidth); double diff = (def / imgwidth); thumbWidth = Convert.ToInt16(Math.Round(diff * oldwidth)); thumbHeight = Convert.ToInt16(Math.Round(diff * oldheight)); } else { thumbWidth = oldwidth; thumbHeight = oldheight; } if ((thumbHeight > height)) { double def = height; double imgheight = Convert.ToDouble(thumbHeight); double diff = (def / imgheight); thumbWidth = Convert.ToInt16(Math.Round(diff * thumbWidth)); thumbHeight = height; } } Size newSize = new Size(thumbWidth, thumbHeight); using (Bitmap newImage = new Bitmap(newSize.Width - 2, newSize.Height - 2, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)) { using (Graphics canvas = Graphics.FromImage(newImage)) { canvas.SmoothingMode = SmoothingMode.AntiAlias; canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; canvas.PixelOffsetMode = PixelOffsetMode.HighQuality; canvas.DrawImage(oldImage, new Rectangle(new Point(-1, -1), newSize)); MemoryStream m = new MemoryStream(); string Ext = Path.GetExtension(strFilePath).ToLower(); if (Ext == ".gif" | Ext == ".png") { newImage.Save(m, System.Drawing.Imaging.ImageFormat.Png); } else { newImage.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg); } return m; } } } } #region "Black And White Image" public static Bitmap MakeGrayScale(Bitmap original) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); //create the grayscale ColorMatrix ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] { 0.3f, 0.3f, 0.3f, 0, 0 }, new float[] { 0.59f, 0.59f, 0.59f, 0, 0 }, new float[] { 0.11f, 0.11f, 0.11f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); //create some image attributes ImageAttributes attributes = new ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); //draw the original image on the new image //using the grayscale color matrix g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return newBitmap; } #endregion } }
Now use below code to create thumbnail from original image.
<img src="thumbnailIHandler.ashx?p=FULLPATHIMAGE&D=214X126X2" />
Example
<img src="thumbnailIHandler.ashx?p=/images/lorem-ipsum-dolor.jpg&D=214X126X2" />