Skip to content

Instantly share code, notes, and snippets.

@rogovski
Created September 24, 2015 21:43
Show Gist options
  • Select an option

  • Save rogovski/aac229ef3c5e7e473dba to your computer and use it in GitHub Desktop.

Select an option

Save rogovski/aac229ef3c5e7e473dba to your computer and use it in GitHub Desktop.
re-size an image in C#
// taken from http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp
// user: Mark
/// <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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment