Image resizing

The .NET GDI+ is not very fast, but it is actually very powerfull. A thing I have used the api for is to create thumbnail images. The GDI is capable of drawing images using both bilinear and bicubic interpolation. This means that your resized image will not have the sharp edges that occurs when resizing without any filters. 

This very small function will resize an image to the specified size:

using System.Drawing;
using System.Drawing.Drawing2D;

private Image CreateThumbnailImage(Image original, Size size)
  // Create new bitmap
  Bitmap b = new Bitmap(size.Width, size.Height);
  // Create a drawing surface 
  Graphics g = Graphics.FromImage(b);
  // Set the interpolationmode
  g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  // Draw the original image to the new image
  g.DrawImage(original, 0, 0, size.Width , size.Height);
  // return the new image
  return b;

Notice that I set the InterpolationMode on the drawing surface. This tells the GDI+ how to draw an image. The HighQualityBicubic interpolationmode is probably the best setting, but also the slowest. You can also use HighQualityBilinear, Bicubic, Linear, or any of the other 9 modes.

Drawing images using an InterpolationMode is very slow, so try not using it in real time.

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in General .NET and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s