|
Resizing an image in C#Posted on 26. Jan, 2010 by Wim Haanstra in C#, Coding. |
For a project I am working on, I was looking for a good way to resize images in a few different ways. I have done image resizing before, but it really feels that I am re-inventing the wheel every time I do this. So now I decided to write a nice static method which can resize an Image to certain dimensions and outputs an Image object again.
The method requires 3 parameters:
1. An image object. I hope you know how to load a file in an Image object (hint: FromFile).
2. A Resolution object, being nothing more than a small class I created, which also contains the width and the height.
3. The mode you want to use for resizing the image. I explained the modes in the enum listed below.
It probably isnt the neatest code you’ve ever seen, so if you have any comments, please let me know. Code and more explanation after the break…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | /// <summary> /// Method resizes the image so it fits the wanted resolution best. /// </summary> /// <param name="image">The image to be resized</param> /// <param name="targetResolution">The resolution which the image should be when the method is ready.</param> /// <param name="resizeMode">The mode for resizing the image.</param> public static System.Drawing.Image Resize(System.Drawing.Image image, Resolution targetResolution, ResizeMode resizeMode) { int sourceWidth = image.Width; int sourceHeight = image.Height; int targetWidth = targetResolution.Width; int targetHeight = targetResolution.Height; // Supplied image is landscape, while the target resolution is portait OR // supplied image is in portait, while the target resolution is in landscape. // switch target resolution to match the image. if ((sourceWidth > sourceHeight && targetWidth < targetHeight) || (sourceWidth < sourceHeight && targetWidth > targetHeight)) { targetWidth = targetResolution.Height; targetHeight = targetResolution.Width; } float ratio = 0; float ratioWidth = ((float)targetWidth / (float)sourceWidth); float ratioHeight = ((float)targetHeight / (float)sourceHeight); if (ratioHeight < ratioWidth) ratio = ratioHeight; else ratio = ratioWidth; Bitmap newImage = null; switch (resizeMode) { case ResizeMode.Normal: default: { int destWidth = (int)(sourceWidth * ratio); int destHeight = (int)(sourceHeight * ratio); newImage = new Bitmap(destWidth, destHeight); using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, 0, 0, destWidth, destHeight); } break; } case ResizeMode.Crop: { if (ratioHeight > ratioWidth) ratio = ratioHeight; else ratio = ratioWidth; int destWidth = (int)(sourceWidth * ratio); int destHeight = (int)(sourceHeight * ratio); newImage = new Bitmap(targetWidth, targetHeight); int startX = 0; int startY = 0; if (destWidth > targetWidth) startX = 0 - ((destWidth - targetWidth) / 2); if (destHeight > targetHeight) startY = 0 - ((destHeight - targetHeight) / 2); using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, startX, startY, destWidth, destHeight); } break; } case ResizeMode.Stretch: { newImage = new Bitmap(targetWidth, targetHeight); using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, 0, 0, targetWidth, targetHeight); } break; } case ResizeMode.Fill: { newImage = new Bitmap(targetWidth, targetHeight); int destWidth = (int)(sourceWidth * ratio); int destHeight = (int)(sourceHeight * ratio); int startX = 0; int startY = 0; if (destWidth < targetWidth) startX = 0 + ((targetWidth - destWidth) / 2); if (destHeight < targetHeight) startY = 0 + ((targetHeight - destHeight) / 2); newImage = new Bitmap(targetWidth, targetHeight); using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, startX, startY, destWidth, destHeight); } break; } } return (System.Drawing.Image)newImage; } |
The resizeMode parameter is of the type ResizeMode, which is nothing more than a simple enumeration, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /// <summary> /// Modes available for the resize method of images. /// </summary> public enum ResizeMode { /// <summary> /// This will resize images to the resolution nearest to the target resolution. Images can become smaller when using this option /// </summary> Normal = 1, /// <summary> /// This will stretch an image so it always is the exact dimensions of the target resolution /// </summary> Stretch = 2, /// <summary> /// This will size an image to the exact dimensions of the target resolution, keeping ratio in mind and cropping parts that can't /// fit in the picture. /// </summary> Crop = 3, /// <summary> /// This will size an image to the exact dimensions of the target resolution, keeping ratio in mind and filling up the image /// with black bars when some parts remain empty. /// </summary> Fill = 4 } |
And the targetResolution parameter, is a simple class containing some properties like Name, Width and Height.
This method seems to do everything I need, if you need anything more, or any information about this code, just email me or reply here.





i recognize some code here and there
Yup I reused some old code of the WallPaper 1.0 server
. Yeah Yeah, thanks