// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Crops an image to the given directions. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Processors { using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using ImageProcessor.Common.Exceptions; using ImageProcessor.Imaging; /// /// Crops an image to the given directions. /// public class Crop : IGraphicsProcessor { /// /// Initializes a new instance of the class. /// public Crop() { this.Settings = new Dictionary(); } /// /// Gets or sets DynamicParameter. /// public dynamic DynamicParameter { get; set; } /// /// Gets or sets any additional settings required by the processor. /// public Dictionary Settings { get; set; } /// /// Processes the image. /// /// /// The current instance of the class containing /// the image to process. /// /// /// The processed image from the current instance of the class. /// public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { int sourceWidth = image.Width; int sourceHeight = image.Height; RectangleF rectangleF; CropLayer cropLayer = this.DynamicParameter; if (cropLayer.CropMode == CropMode.Percentage) { // Work out the percentages. float left = cropLayer.Left * sourceWidth; float top = cropLayer.Top * sourceHeight; float width = (1 - cropLayer.Left - cropLayer.Right) * sourceWidth; float height = (1 - cropLayer.Top - cropLayer.Bottom) * sourceHeight; rectangleF = new RectangleF(left, top, width, height); } else { rectangleF = new RectangleF(cropLayer.Left, cropLayer.Top, cropLayer.Right, cropLayer.Bottom); } Rectangle rectangle = Rectangle.Round(rectangleF); if (rectangle.X < sourceWidth && rectangle.Y < sourceHeight) { if (rectangle.Width > (sourceWidth - rectangle.X)) { rectangle.Width = sourceWidth - rectangle.X; } if (rectangle.Height > (sourceHeight - rectangle.Y)) { rectangle.Height = sourceHeight - rectangle.Y; } newImage = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb); using (Graphics graphics = Graphics.FromImage(newImage)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 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 surrounding 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.DrawImage( image, new Rectangle(0, 0, rectangle.Width, rectangle.Height), rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, GraphicsUnit.Pixel, wrapMode); } } // Reassign the image. image.Dispose(); image = newImage; } } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; } } }