// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Applies a mask to the given image. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Processors { using System; using System.Collections.Generic; using System.Drawing; using ImageProcessor.Common.Exceptions; /// /// Applies a mask to the given image. /// public class Mask : IGraphicsProcessor { /// /// Initializes a new instance of the class. /// public Mask() { this.Settings = new Dictionary(); } /// /// Gets or sets the dynamic parameter. /// 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; Size original = image.Size; Size smaller = new Size(image.Width / 2, image.Height / 2); int x = (original.Width - smaller.Width) / 2; int y = (original.Height - smaller.Height) / 2; int width = image.Width; int height = image.Height; try { newImage = new Bitmap(original.Width, image.Height); using (Graphics graphics = Graphics.FromImage(newImage)) { graphics.DrawImage(image, x, y, smaller.Width, smaller.Height); } 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; } } }