beta.blog

C# – Resize Images

by on Feb.15, 2012, under Programming

Short article describing how to resize Images/Bitmaps in C-Sharp:

[csharp]
/// <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 System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
// This Bitmap will hold our resized Image
Bitmap result = new Bitmap(width, height);

// We’re going to use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
// Set the quality of the resized picture to ‘HighQuality’
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

// Draw the image into the target Bitmap
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}

// Return the resulting Bitmap
return result;
}
[/csharp]


Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!