Created
September 24, 2015 21:43
-
-
Save rogovski/aac229ef3c5e7e473dba to your computer and use it in GitHub Desktop.
re-size an image in C#
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
| // 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