Created
March 29, 2022 06:16
-
-
Save mirmostafa/78b391d4ac75a16635cf22aa820d55c9 to your computer and use it in GitHub Desktop.
Resize the image to the specified width and height.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <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