ASP.NET Image Handler To Resize and Preview High Quality Images in Real Time

01 November 2011

In this article we will provide information and sample codes for creating image handler file in asp.net, c# or vb.net which can be used to resize image, generate highest quality output and show image preview in real time.

You can directly use image handler in image src path to process and display output on browser.

Such handler file have lots of advantages, e.g

  • Provide ability to resize image in real time and display output on browser.
  • Protect image direct path from access.
  • Add lots of other validation to protect images from anti leeching.

Image handling have some drawbacks

  • Consume resources and performance issues so should be used in cases where its needed. e.g if you provide system for user to prepare image for mobile photo, iphone, psp, high definition, and according to different screen resolutions.

Handler file can be called as

<img src="imagehandler.ashx?filename=abc.jpg"...

Code for generating image handler.

GenerateThumbnail function will be used to generate highest possible quality output based on three paramters,

lcFilename (complete path of source photo), width (output width of photo), height (output height of photo)

Code is shown below

private byte[] GenerateThumbnail(string lcFilename, int Width, int Height)

{
        //Image img = Image.FromStream(fStream);
        //Image thumbnailImage = img.GetThumbnailImage(Width, height),
        //      new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

        Bitmap loBMP = null;
        Bitmap bmpOut = null;
        try
        {
            loBMP = new Bitmap(lcFilename);
            ImageFormat loFormat = loBMP.RawFormat;
            Size newSize = new Size();
            newSize.Height = Height;
            newSize.Width = Width;
            bmpOut = new Bitmap(newSize.Width, newSize.Height);
            Graphics canvas = Graphics.FromImage(bmpOut);
            canvas.SmoothingMode = SmoothingMode.AntiAlias;
            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
            canvas.DrawImage(loBMP, new Rectangle(new Point(0, 0), newSize));
        }
        catch (Exception ex)
        {
           
        }
       
        MemoryStream imageStream = new MemoryStream();
        bmpOut.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

        byte[] imageContent = new Byte[imageStream.Length];
        imageStream.Position = 0;
        imageStream.Read(imageContent, 0, (int)imageStream.Length);
        return imageContent;
    }

GenerateThumbnail function to be called in ProcesRequest method to output resized thumb.

if (File.Exists(strPath))

{
     ht = new Hashtable();
     //FileStream fInfo = new FileStream(strPath, FileMode.Open);
     byte[] bFile;
     bFile = GenerateThumbnail(strPath, width, height);
     ht.Add(this.ImageID, bFile);
     //fInfo.Close();
}

Byte[] arrImg = (byte[])ht[this.ImageID];
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(arrImg);
context.Response.End();

Hope this code will help you generate image handler with highest possible image output.