Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp ASP.NET Resize Image High Quality

ASP.NET Resize Image High Quality

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:

Click on the image to see it in original size.

2. Its thumbnails:

+ 600×400:

High quality thumbnail 600x400

+ 190×120:

High quality thumbnail 190x120

1. Original picture:

Click on the image to see it in original size.

2. Its thumbnails:

+ 120×90:

High quality thumbnail 120x90

+ 600×480:

High quality thumbnail 600x480

Dec 6, 2011Hoan Huynh
Most Popular Video And Photo File ExtensionChange Phone Name Of iPhone 4s
You Might Also Like:
  • C# Generate Website Screenshot And Save Thumbnail
  • C# Save Web Page URL To Image
  • Share Page On Facebook With Thumbnail Featured Image
  • ASP.Net C# Download Or Save Image File From URL
  • Resize picture by using Paint
  • C# Get File Extension Without Sub String Or Split Function
  • PHP Get Image Width And Height
  • C# Read File Content
  • Get System Information: CPU, RAM, Hard Disks, VGA Card with Msinfo32.exe On Windows 7
  • WordPress View Large Image Without Leaving Current Page
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago CSharpBitmap, Drawing, Drawing2D, HighQuality, Thumbnail301
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,459 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,839 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,655 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,994 views
C# Read Json From URL And Parse/Deserialize Json
11,722 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development