About Author
|
 |
Chintankumar Patel
09 Jun, 2008
|
 |
Contact Me
|
Working as a Technical Consultant for Conchango.
Having experience in to IT from 7+ years and working on Microsoft Technologies
|
Tags
|
|
.Net , .Net Framework 2.0 , .Net Graphics , Asp.Net , Boot , C# , Class Library , Coding Standards , Convert File System , Database , DATEDIFF , DATEDIFF Function , Disk Management , Embedded Resources , EnableEventValidation , EnableViewState , EncoderParameter , FAT32 to NTFS , Form Authentication , HDD , High Quality Thumbnail , Intellisense , Linux , Microsoft Access , Microsoft's SQL , Partition , ReadyBoost , ReSharper , Resize Image , Resize Partition , Security , SQLDataReader , SuperFetch , USB , Usb device not recognized , Vista , Visual Studio 2005 , What's New , Windows , Windows Application , Windows Errors , Windows Vista , Windows XP , YouTube
|
|
Create High Quality Thumbnails/Resize using System.Drawing.Graphics - ASP.Net C# Code
|
Wed, 18 Jun, 2008
|
|
|
This post will help you to create High Quality Thumbnails or can Resize the image The GetThumbnailImage() method of Bitmap class produces a thumbnail image from the file specified. Life is so easy with it. Not always though. Sometimes the thumbnails produced are low quality - pixelated and blurred
Why it Happens?
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
First of all you will need to include the reference of following namespaces
using System.Drawing;
using System.Drawing.Design;
Use the following code to create High Quality Thumbnail/Resize the image.
string originalFilePath = "C:\\originalimage.jpg"; //Replace with your image path
string thumbnailFilePath = string.Empty;
Size newSize = new Size(120,90); // Thumbnail size (width = 120) (height = 90)
using (Bitmap bmp = new Bitmap(originalFilePath))
{
thumbnailFilePath = "C:\\thumbnail.jpg"; //Change the thumbnail path if you want
using (Bitmap thumb = new Bitmap((System.Drawing.Image)bmp, newSize))
{
using (Graphics g = Graphics.FromImage(thumb)) // Create Graphics object from original Image
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//Set Image codec of JPEG type, the index of JPEG codec is "1"
System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
//Set the parameters for defining the quality of the thumbnail... here it is set to 100%
System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
//Now draw the image on the instance of thumbnail Bitmap object
g.DrawImage(bmp, new Rectangle(0, 0, thumb.Width, thumb.Height));
thumb.Save(thumbnailFilePath, codec, eParams);
}
}
}
|
Create High Quality Thumbnails/Resize using System.Drawing.Graphics - ASP.Net C# Code
|
|
Comments
|
|
Re : Create High Quality Thumbnails/Resize using System.Drawing.Graphics - ASP.Net C# Code
Mon, 11 Aug, 2008 - 21:03:56 by Tim
|
|
| |
This is a great example and I'm emplying a similar method. However, how does this take into account for portrait images? This is the part I'm struggling with. Tim
|
| |
|
Re : Create High Quality Thumbnails/Resize using System.Drawing.Graphics - ASP.Net C# Code
Tue, 12 Aug, 2008 - 09:43:57 by Chintankumar Patel
|
|
| |
Hi. could you please deeply explain your requirements??
|
| |
|
Re : Create High Quality Thumbnails/Resize using System.Drawing.Graphics - ASP.Net C# Code
Tue, 12 Aug, 2008 - 15:38:53 by Deepak
|
|
| |
Hai, Very gud piece of code. Expecting more from u.
|
| |
|
Re : Create High Quality Thumbnails/Resize using System.Drawing.Graphics - ASP.Net C# Code
Tue, 12 Aug, 2008 - 16:44:17 by Tim
|
|
| |
When working with portrait images your 120x90 thumb would not work it would skew the image because it's taller than it is wide i.e. 600x800.
I am try to do just such a thing but resizing the images to multiple sizes not just to a small thumb. I've posted on asp.net forums but with no luck in getting help. It has exmaple code though it has a few aparent glitches. Perhars you could review and give me your input. http://forums.asp.net/t/1303342.aspx
|
| |
|
|
|
Re : Create High Quality Thumbnails/Resize using System.Drawing.Graphics - ASP.Net C# Code
Wed, 27 Aug, 2008 - 18:48:49 by Merlinox
|
|
| |
All good. I did the same thing. But resizing a pics of 26 KB, I obtain a new file of 464 KB. I think it's no jpeg compressed.
|
| |
|
Re : Create High Quality Thumbnails/Resize using System.Drawing.Graphics - ASP.Net C# Code
Sat, 08 Nov, 2008 - 17:13:40 by canhtq
|
|
| |
cool!!! thanks!
|
| |
|
|
|
|