If you allow people be able to upload their photos to your web application or you want to capture screenshot of a particular web page via its URL, you need to think about resizing them or generating thumbnail to a desired width and height with 2 key purposes: reduce its size as well as optimize the loading speed (especially when you display them in your gallery or somewhere on your website) and ensure they’re fit with your design.
By resizing image with high quality, you can save significant your disk space while still keep user experience. A ASP.NET (C#) function below will re-size image then save its best quality thumbnail to hard drive. And this function should work well with almost popular image extension such as: .jpg, .png, etc.
1. C# Resize Image With High Quality Thumbnail
[csharp] using System;using System.Drawing;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class resize_image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
save_thumbnail(Server.MapPath("Koala.jpg"), Server.MapPath("Koala-thumb_600x400.jpg"), 600, 400);
save_thumbnail(Server.MapPath("Koala.jpg"), Server.MapPath("Koala-thumb_190x120.jpg"), 190, 120);
save_thumbnail(Server.MapPath("web-screen-shot.png"), Server.MapPath("web-screen-shot-thumb_120x90.png"), 120, 90);
save_thumbnail(Server.MapPath("web-screen-shot.png"), Server.MapPath("web-screen-shot-thumb_600x480.png"), 600, 480);
}
public void save_thumbnail(string source_file, string thumb_file, int width, int height)
{
System.Drawing.Image pic_tmp = System.Drawing.Image.FromFile(source_file);
int src_width = pic_tmp.Width;
int src_height = pic_tmp.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)width / (float)src_width);
nPercentH = ((float)height / (float)src_height);
if (width == 0)//Auto width
{
nPercent = nPercentH;
}
else if (height == 0)//auto height
{
nPercent = nPercentW;
}
else if (nPercentW > nPercentH)
{
nPercent = nPercentH;
}
else
{
nPercent = nPercentW;
}
int thumb_width = 0;
int thumb_height = 0;
if (nPercent < 1)
{
thumb_width = (int)(src_width * nPercent);
thumb_height = (int)(src_height * nPercent);
Bitmap bmp = new Bitmap(thumb_width, thumb_height);
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, thumb_width, thumb_height);
gr.DrawImage(pic_tmp, rectDestination, 0, 0, src_width, src_height, GraphicsUnit.Pixel);
bmp.Save(thumb_file);
bmp.Dispose();
}
else //Just make a copy
{
System.IO.File.Copy(source_file, thumb_file);
}
pic_tmp.Dispose();
}
}
[/csharp]
2. Preview the quality of resized images
1. Original picture:
2. Its thumbnails:
+ 600×400:
+ 190×120:
1. Original picture:
2. Its thumbnails:
+ 120×90:
+ 600×480: