This article will help you create high quality thumbnail from an image as well as resize image dynamically.
GetThumbnailImage method of class Bitmap produces a thumbnail image from the file specified. Life is so easy with it. Not always though. Sometimes the thumbnail produced has low quality - pixelated and blurred.
Why it Happens? Why My Images are Pixelated and Blurred?
Image formats like jpeg may store the thumbnail inside the same file. If we use System.Drawing.Bitmap method GetThumbnailImage, method checks if there’s a thumbnail image stored into the file and, if the thumb is found, it returns that thumbnail version scaled to the width and height you requested. If the thumbnail
version of the image is smaller then the size you requested to produce,
thats when problem occurs. The thumbnails produced become pixelated as
we know stretching an image to a larger once reduces the Image Quality.
Solution:
Solution is really simple. We will load the source image to a System.Drawing.Image object, scale it to the desired width and height preserving good quality and save it to a new file.
Step by Step Procedure to Create a Good Quality Thumbnail:
For this example lets assume these variables
String src="c:/myImages/a.jpg"; //absolute location of source image
String dest="c:/myImages/a_th.jpg"; //absolute location of the new image created(thumbnail)
int thumbWidth=132; //width of the image (thumbnail) to produce
The code snippest shown here is in
ASP .NET C#.
You have to use these namespaces
using System.Drawing;
using System.Drawing.Drawing2D;
Following are the Steps along with example code:
- Get the source image to a System.Drawing.Image object
System.Drawing.Image image = System.Drawing.Image.FromFile(src);
- Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.
int srcWidth=image.Width;
int srcHeight=image.Height;
//we will get the sizeratio in decimal so that we dont get exception in case width is less then height.
Decimal sizeRatio = ((Decimal)srcHeight/srcWidth);
int thumbHeight=Decimal.ToInt32(sizeRatio*thumbWidth);
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
- Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
- Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- Set the System.Drawing.Graphics object property InterpolationMode to High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
- Draw the original image into the target Graphics object scaling to the desired width and height
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
- Save to destination file
bmp.Save(dest);
- dispose / release resources
bmp.Dispose();
image.Dispose();
Lets help each other:
I get emails often asking for help with the code. I always love to do that. Unfortunately sometimes I get too busy. I have a full-time job, maintain two sites, and married. If you have problems with this code, please post on the comment. Anybody interested helping others may reply on the comments as well.
Resources:
http://www.WebCosmo.com 100% Free Classified Advertising
Free Advertising on our
website www.WebCosmo.com for jobs,
apartments,
personals,
pets
and much more in more then 30 countries.