Results 1 to 15 of 19
-
05-06-2008, 09:02 PM #1
- Join Date
- Apr 2008
- Location
- Boston, USA
- Posts
- 13,172
- Thanks
- 744
- Thanked 699 Times in 550 Posts
- Blog Entries
- 4
- Feedback Score
- 4 (100%)
Create High Quality Thumbnail - Resize Image Dynamically - ASP .Net C# Code
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;
int thumbHeight=(srcHeight/srcWidth)*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.HighQu ality;
- 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();
All codes together look like this:
Source: Create High Quality Thumbnail – Resize Image Dynamically – ASP .Net C# CodeCode:System.Drawing.Image image = System.Drawing.Image.FromFile(src); int srcWidth=image.Width; int srcHeight=image.Height; int thumbHeight=(srcHeight/srcWidth)*thumbWidth; Bitmap bmp = new Bitmap(thumbWidth, thumbHeight); System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp); gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality ; gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight); gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel); bmp.Save(dest); bmp.Dispose(); image.Dispose();
Free Classified Ads & BUSINESS/PROFESSIONAL SOCIAL NETWORK
-
The Following User Says Thank You to manik For This Useful Post:
sajjad ali khan (04-21-2011)
-
04-17-2009, 02:22 AM #2
Sophomore
- Join Date
- Mar 2009
- Posts
- 79
- Thanks
- 0
- Thanked 1 Time in 1 Post
- Feedback Score
- 0
Thank for the post...
-
04-21-2009, 05:27 PM #3
Freshman
- Join Date
- Apr 2009
- Posts
- 6
- Thanks
- 0
- Thanked 0 Times in 0 Posts
- Feedback Score
- 0
Need some time to try it out,thank for the tutorial..

-
06-09-2009, 01:25 PM #4
Freshman
- Join Date
- Jun 2009
- Posts
- 5
- Thanks
- 0
- Thanked 0 Times in 0 Posts
- Feedback Score
- 0
To me, it seems a little more complicated. However, thanks for the post.
-
08-31-2009, 04:21 PM #5
Junior
- Join Date
- Sep 2008
- Posts
- 155
- Thanks
- 1
- Thanked 4 Times in 4 Posts
- Feedback Score
- 0
This is a very useful post for .Net/ C# programmers wanting to create thumbnails. Non-programmers could try using this free web page thumbnail generator.
-
The Following User Says Thank You to MikeL For This Useful Post:
manik (08-31-2009)
-
09-01-2009, 03:09 AM #6
- Join Date
- Jun 2008
- Location
- www.brainpulse.com
- Posts
- 5,593
- Thanks
- 170
- Thanked 429 Times in 296 Posts
- Blog Entries
- 5
- Feedback Score
- 0
wow great post book mark it thanks manik for providing very useful code!!!
Web Hosting India- Host Away with BrainPulse Hosting Solutions. SEO Company India- offers, Guaranteed SEO Services as a stand-alone Email Marketing Solutions- Complete Mass mailing
-
10-06-2009, 10:15 PM #7
Freshman
- Join Date
- Oct 2009
- Posts
- 1
- Thanks
- 0
- Thanked 0 Times in 0 Posts
- Feedback Score
- 0
That's pretty good.
Many thanks to ur post. I love it.
Would a new one like me be welcome here?
Thanks so much in deed.
-
10-06-2009, 11:15 PM #8
- Join Date
- Apr 2008
- Location
- Boston, USA
- Posts
- 13,172
- Thanks
- 744
- Thanked 699 Times in 550 Posts
- Blog Entries
- 4
- Feedback Score
- 4 (100%)
-
The Following User Says Thank You to manik For This Useful Post:
sajjad ali khan (04-21-2011)
-
12-28-2009, 04:34 PM #9
Nice snippet Manik.
What version of the framework is this written against?
-
02-02-2010, 12:17 AM #10
- Join Date
- Apr 2008
- Location
- Boston, USA
- Posts
- 13,172
- Thanks
- 744
- Thanked 699 Times in 550 Posts
- Blog Entries
- 4
- Feedback Score
- 4 (100%)
-
03-03-2010, 05:59 AM #11
Freshman
- Join Date
- Mar 2010
- Posts
- 7
- Thanks
- 0
- Thanked 0 Times in 0 Posts
- Feedback Score
- 0
Nice but a little bit complicated
-
07-28-2010, 08:41 PM #12
Freshman
- Join Date
- Jul 2010
- Posts
- 28
- Thanks
- 0
- Thanked 0 Times in 0 Posts
- Feedback Score
- 0
excellent excellent just as usual!!! =)=)=)
-
09-30-2010, 07:20 AM #13
Freshman
- Join Date
- Sep 2010
- Posts
- 3
- Thanks
- 0
- Thanked 0 Times in 0 Posts
- Feedback Score
- 0
Create High Quality Thumbnail - Resize Image Dynamically - ASP .Net C# Code
This article will help us create high quality thumbnail from an image as well as resize image dynamically.Thank for the post...
-
12-17-2010, 05:36 AM #14
Freshman
- Join Date
- Dec 2010
- Posts
- 43
- Thanks
- 0
- Thanked 0 Times in 0 Posts
- Feedback Score
- 0
Creatre high quality thumbnail from an image as well as resize image dynamically Get ThumbnailImage method of class Bitmap produces a thumbnail image from the file specified.Thanks for sharing info.
-
01-05-2011, 09:55 PM #15
Image quality is very bad


Reply With Quote




