This will perform a high quality resize:
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
wrapMode.SetWrapMode(WrapMode.TileFlipXY)prevents ghosting around the image borders — naïve resizing will sample transparent pixels beyond the image boundaries, but by mirroring the image we can get a better sample (this setting is very noticeable)destImage.SetResolutionmaintains DPI regardless of physical size — may increase quality when reducing image dimensions or when printing- Compositing controls how pixels are blended with the background — might not be needed since we’re only drawing one thing.
graphics.CompositingModedetermines whether pixels from a source image overwrite or are combined with background pixels.SourceCopyspecifies that when a color is rendered, it overwrites the background color.graphics.CompositingQualitydetermines the rendering quality level of layered images.
graphics.InterpolationModedetermines how intermediate values between two endpoints are calculatedgraphics.SmoothingModespecifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing) — probably only works on vectorsgraphics.PixelOffsetModeaffects rendering quality when drawing the new image
Maintaining aspect ratio is left as an exercise for the reader (actually, I just don’t think it’s this function’s job to do that for you).
Also, this is a good article describing some of the pitfalls with image resizing. The above function will cover most of them, but you still have to worry about saving.