public static Image ResizeImage(Image image, int maxWidthOrHeight) { ExceptionUtils.ThrowIfZeroOrNegative(maxWidthOrHeight, "maxWidthOrHeight"); if ((image.Width <= maxWidthOrHeight) && (image.Height <= maxWidthOrHeight)) { return(image); } int newWidth, newHeight; if (image.Width > image.Height) { newWidth = maxWidthOrHeight; newHeight = (image.Height * maxWidthOrHeight) / image.Width; if (newHeight < 0) { newHeight = 1; } } else { newWidth = (image.Width * maxWidthOrHeight) / image.Height; newHeight = maxWidthOrHeight; if (newWidth < 0) { newWidth = 1; } } return(ResizeImage(image, newWidth, newHeight)); }
public static Image ResizeImage(Image image, int width, int height) { ExceptionUtils.ThrowIfZeroOrNegative(width, "width"); ExceptionUtils.ThrowIfZeroOrNegative(height, "height"); if ((image.Width != width) || (image.Height != height)) { Bitmap resizeImage = new Bitmap(width, height, PixelFormat.Format24bppRgb); using (Graphics graphics = System.Drawing.Graphics.FromImage(resizeImage)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.DrawImage(image, 0, 0, width, height); } return(resizeImage); } else { return(image); } }