Skip to content

Instantly share code, notes, and snippets.

@mirmostafa
Created March 29, 2022 06:16
Show Gist options
  • Select an option

  • Save mirmostafa/78b391d4ac75a16635cf22aa820d55c9 to your computer and use it in GitHub Desktop.

Select an option

Save mirmostafa/78b391d4ac75a16635cf22aa820d55c9 to your computer and use it in GitHub Desktop.
Resize the image to the specified width and height.
/// <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)
{
//a holder for the result
var result = new Bitmap(width, height);
//set the resolutions the same to avoid cropping due to resolution differences
result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//use a graphics object to draw the resized image into the bitmap
using (var graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment