Mike Borozdin's Blog

A blog about programming, web and IT in general

High Quality Image Resizing with .NET

I want to proceed with telling how to deal with uploaded images with ASP.NET. I have already showed how to resize images. However, that resizing method isn't perfect. It doesn't produce images of high quality. Even though it just shrinks images, the quality of resized images don't match the quality of original images, the produced quality usually appears to be worse. Sure, there is a neat solution. We just need to use the HighQualityBicubic Interpolation mode. Sounds difficult? In fact, it's not difficult to implement, just need to add a few lines of code,  but the result is great.

Here are two images, the left is made with a simply resizing method, while the right one is made with a resizing method. Note, that it doesn't have noise on the buildings and the right image has the better contrast.

low high

The code is:

private Bitmap ResizeImage(Stream streamImage, int maxWidth, int maxHeight)
{
    Bitmap originalImage = new Bitmap(streamImage);
    int newWidth = originalImage.Width;
    int newHeight = originalImage.Height;
    double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;

    if (aspectRatio <= 1 && originalImage.Width > maxWidth)
    {
        newWidth = maxWidth;
        newHeight = (int)Math.Round(newWidth / aspectRatio);
    }
    else if (aspectRatio > 1 && originalImage.Height > maxHeight)
    {
        newHeight = maxHeight;
        newWidth = (int)Math.Round(newHeight * aspectRatio);
    }

    Bitmap newImage = new Bitmap(originalImage, newWidth, newHeight);
    
    Graphics g = Graphics.FromImage(newImage);
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
    g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);

    originalImage.Dispose();
    
    return newImage;     
}

You can read more on different interpolation mode here.

You can also download the project files here, they include uploading and high quality resizing.


Tags:
Posted by Mike Borozdin on Monday, August 25, 2008 2:53 AM GMT
  Shout it Kick it!  
Permalink | Comments (12) | Post RSSRSS comment feed

Comments

DotNetKicks.com

Sunday, August 24, 2008 7:57 PM GMT

trackback

Trackback from DotNetKicks.com

High Quality Image Resizing with .NET

Shawn Miller United States

Monday, August 25, 2008 5:55 AM GMT

Shawn Miller

Haven't tested it, but does this code leak memory?  Seems like at the very least there should be a originalImage.Dispose() in there somewhere.

Mike Borozdin Russia

Monday, August 25, 2008 6:34 AM GMT

Mike Borozdin

Thanks for pointing that out, Shawn Smile!

Mange Sweden

Monday, August 25, 2008 9:51 PM GMT

Mange

How about using the built-in features of the C# language right away and use "using (Graphics g = ....".

Duror Turkey

Sunday, December 21, 2008 7:41 AM GMT

Duror

Thank you ...

Web Radio

Friday, December 26, 2008 10:16 PM GMT

trackback

Trackback from Web Radio

Web Radio

hilpers.it

Saturday, January 17, 2009 12:51 PM GMT

pingback

Pingback from hilpers.it

GDI e definizione delle immagini | hilpers

Nathanael Jones United States

Monday, January 19, 2009 6:25 AM GMT

Nathanael Jones

Compositing mode is also important - adding these lines usually brings the image up to Photoshop standards.

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality

For a stable resizing module with disk caching, URL-based resizing, and *all* of the source code included, see

nathanaeljones.com/products/asp-net-image-resizer/

Mike Borozdin

Monday, January 19, 2009 11:01 AM GMT

Mike Borozdin

Thanks Nathanael for the useful information Smile!

Nathanael Jones United States

Tuesday, February 03, 2009 7:44 AM GMT

Nathanael Jones

No problem. Smile

I'd also drop a using(Bitmap originalImage = new Bitmap(streamImage)){ around the method, and also put using(){} on the Graphics instance too.  

You don't want a bad stream or invalid image to cause a memory leak... better safe than sorry Smile

Mustafa Yolcu Turkey

Monday, August 24, 2009 1:46 AM GMT

Mustafa Yolcu

hi,

anything changed with these codes.

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

when i resize my image with photoshop, image will be sharpener than this images. these codes image is smoother then photoshops.. what can we do for this. it must be same of photoshops.

thanks.


lucisferre

Friday, January 22, 2010 10:58 AM GMT

trackback

Even images need boundaries

Even images need boundaries

Comments are closed