web 2.0

Useful Image Thumbnails Generator HttpHandler for ASP.NET

Hi,

Today i'll talking about a pretty useful thing we all needs in ASP.NET development :
Generate Image Thumbnails with ratio keeping and JPEG Compression Optimization.

Source Image : 636x476 - 54ko

01SC087

Thumbnail Image : 200x149 - 9ko

ThumbImageHandler.ashx

How it works ?

Source Image is loaded with the System.Drawing.Bitmap class and resized by hand with a System.Drawing.Graphics.

Then we create a custom System.Drawing.Imagine.ImageCodecInfo and System.Drawing.Imaging.EncoderParameter with a Compression ratio set to 75, which seems to be pretty good generally.

Finally the newly created resized Bitmap image is saved to the specified System.IO.Stream.

Here is the ImageThumbnailer class.

public class ImageThumbnailer
    {
        private Bitmap _thumb = null;

        /// <summary>
        /// Creates the thumbnail.
        /// </summary>
        /// <param name="SourceImage">The source image.</param>
        /// <param name="Width">The width.</param>
        /// <param name="Height">The height.</param>
        /// <param name="KeepRatio">if set to <c>true</c> [keep ratio].</param>
        /// <returns></returns>
        public Bitmap CreateThumbnail(Bitmap SourceImage,
            Int32 Width, Int32 Height, Boolean KeepRatio)
        {
            // if Source Bitmap smaller than designated thumbnail => Return Original
            if (SourceImage.Width < Width && SourceImage.Height < Height)
                return SourceImage;

            try
            {
                Int32 _Width = 0;
                Int32 _Height = 0;

                _Width = Width;
                _Height = Height;

                if (KeepRatio)
                {
                    if (SourceImage.Width > SourceImage.Height)
                    {
                        _Width = Width;
                        _Height = (Int32)(SourceImage.Height *
                            ((Decimal)Width / SourceImage.Width));
                    }
                    else
                    {
                        _Height = Height;
                        _Width = (Int32)(SourceImage.Width *
                            ((Decimal)Height / SourceImage.Height));
                    }
                }

                _thumb = new Bitmap(_Width, _Height);
                using (Graphics g = Graphics.FromImage(_thumb))
                {
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.FillRectangle(Brushes.White, 0, 0, _Width, _Height);
                    g.DrawImage(SourceImage, 0, 0, _Width, _Height);
                }
            }
            catch
            {
                _thumb = null;
            }
            return _thumb;
        }

        /// <summary>
        /// Saves the Bitmap to the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        public void Save(Stream stream)
        {
            // JPEG Optimizing
            EncoderParameters encoderParams = new EncoderParameters();
            long[] quality = new long[1];
            quality[0] = 75;
            EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;

            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo codec = encoders.Where(
                p => p.FormatDescription.Equals("JPEG")).SingleOrDefault();

            // Save to Specified Stream
            _thumb.Save(stream, codec, encoderParams);
        }
    }

Here is a simple HttpHandler using our ImageThumbnailer class.

public class ThumbImageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpResponse Response = context.Response;
            String ImageUrl = HttpContext.Current.Server.MapPath("~/01SC087.JPG");
            
            
            Bitmap bmp = (Bitmap)Bitmap.FromFile(ImageUrl);
            ImageThumbnailer thumbnailer = new ImageThumbnailer();
            thumbnailer.CreateThumbnail(bmp, 200, 200, true);

            Response.ContentType = "image/jpeg";
            thumbnailer.Save(Response.OutputStream);
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }

 

Hope this help's !



Views(1566)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

ASP.NET | C#

Comments

Add comment


 

biuquote
Loading



Technorati Profile