// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // The resize base for inheriting resizable methods from. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Processors { #region Using using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Text.RegularExpressions; using ImageProcessor.Imaging; #endregion /// /// The resize base for inheriting resizable methods from. /// public abstract class ResizeBase : IGraphicsProcessor { #region IGraphicsProcessor Members /// /// Gets the regular expression to search strings for. /// public abstract Regex RegexPattern { get; } /// /// Gets or sets DynamicParameter. /// public abstract dynamic DynamicParameter { get; set; } /// /// Gets or sets the order in which this processor is to be used in a chain. /// public abstract int SortOrder { get; protected set; } /// /// Gets or sets any additional settings required by the processor. /// public abstract Dictionary Settings { get; set; } /// /// The position in the original string where the first character of the captured substring was found. /// /// /// The query string to search. /// /// /// The zero-based starting position in the original string where the captured substring was found. /// public abstract int MatchRegexIndex(string queryString); /// /// Processes the image. /// /// /// The the current instance of the class containing /// the image to process. /// /// /// The processed image from the current instance of the class. /// public abstract Image ProcessImage(ImageFactory factory); /// /// The resize image. /// /// /// The the current instance of the class containing /// the image to process. /// /// /// The width to resize the image to. /// /// /// The height to resize the image to. /// /// /// The default max width to resize the image to. /// /// /// The default max height to resize the image to. /// /// /// The background color to pad the image with. /// /// /// The mode with which to resize the image. /// /// /// The anchor position to place the image at. /// /// /// The processed image from the current instance of the class. /// protected Image ResizeImage( ImageFactory factory, int width, int height, int defaultMaxWidth, int defaultMaxHeight, Color backgroundColor, ResizeMode resizeMode = ResizeMode.Pad, AnchorPosition anchorPosition = AnchorPosition.Center) { Bitmap newImage = null; Image image = factory.Image; try { int sourceWidth = image.Width; int sourceHeight = image.Height; int destinationWidth = width; int destinationHeight = height; int maxWidth = defaultMaxWidth > 0 ? defaultMaxWidth : int.MaxValue; int maxHeight = defaultMaxHeight > 0 ? defaultMaxHeight : int.MaxValue; // Fractional variants for preserving aspect ratio. double percentHeight = Math.Abs(height / (double)sourceHeight); double percentWidth = Math.Abs(width / (double)sourceWidth); int destinationX = 0; int destinationY = 0; // Change the destination rectangle coordinates if padding and // there has been a set width and height. if (resizeMode == ResizeMode.Pad && width > 0 && height > 0) { double ratio; if (percentHeight < percentWidth) { ratio = percentHeight; destinationX = (int)((width - (sourceWidth * ratio)) / 2); destinationWidth = (int)Math.Floor(sourceWidth * percentHeight); } else { ratio = percentWidth; destinationY = (int)((height - (sourceHeight * ratio)) / 2); destinationHeight = (int)Math.Floor(sourceHeight * percentWidth); } } // Change the destination rectangle coordinates if cropping and // there has been a set width and height. if (resizeMode == ResizeMode.Crop && width > 0 && height > 0) { double ratio; if (percentHeight < percentWidth) { ratio = percentWidth; switch (anchorPosition) { case AnchorPosition.Top: destinationY = 0; break; case AnchorPosition.Bottom: destinationY = (int)(height - (sourceHeight * ratio)); break; default: destinationY = (int)((height - (sourceHeight * ratio)) / 2); break; } destinationHeight = (int)Math.Floor(sourceHeight * percentWidth); } else { ratio = percentHeight; switch (anchorPosition) { case AnchorPosition.Left: destinationX = 0; break; case AnchorPosition.Right: destinationX = (int)(width - (sourceWidth * ratio)); break; default: destinationX = (int)((width - (sourceWidth * ratio)) / 2); break; } destinationWidth = (int)Math.Floor(sourceWidth * percentHeight); } } // If height or width is not passed we assume that the standard ratio is to be kept. if (height == 0) { destinationHeight = (int)Math.Floor(sourceHeight * percentWidth); height = destinationHeight; } if (width == 0) { destinationWidth = (int)Math.Floor(sourceWidth * percentHeight); width = destinationWidth; } if (width > 0 && height > 0 && width <= maxWidth && height <= maxHeight) { // Don't use an object initializer here. // ReSharper disable once UseObjectOrCollectionInitializer newImage = new Bitmap(width, height, PixelFormat.Format32bppPArgb); newImage.Tag = image.Tag; using (Graphics graphics = Graphics.FromImage(newImage)) { // We want to use two different blending algorithms for enlargement/shrinking. // Bicubic is better enlarging for whilst Bilinear is better for shrinking. // http://www.codinghorror.com/blog/2007/07/better-image-resizing.html if (image.Width < destinationWidth && image.Height < destinationHeight) { // We are making it larger. graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; } else { // We are making it smaller. graphics.SmoothingMode = SmoothingMode.None; graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; } // An unwanted border appears when using InterpolationMode.HighQualityBicubic to resize the image // as the algorithm appears to be pulling averaging detail from surFlooring pixels beyond the edge // of the image. Using the ImageAttributes class to specify that the pixels beyond are simply mirror // images of the pixels within solves this problem. using (ImageAttributes wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.Clear(backgroundColor); Rectangle destRect = new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight); graphics.DrawImage(image, destRect, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel, wrapMode); } // Reassign the image. image.Dispose(); image = newImage; } } } catch { if (newImage != null) { newImage.Dispose(); } } return image; } #endregion } }