From bb1f833eaecfe8a05e3de8f61dcd8a04ba6d1f42 Mon Sep 17 00:00:00 2001 From: James South Date: Thu, 9 Oct 2014 23:23:44 +0100 Subject: [PATCH] Begin Mask Former-commit-id: 81e77429f88b216ae3f43f5ae328feef8a67544f Former-commit-id: 7f4231196f9f64095460d0659287396b2c5eb36d --- src/ImageProcessor.Playground/Program.cs | 7 +- src/ImageProcessor/ImageFactory.cs | 11 +++ src/ImageProcessor/ImageProcessor.csproj | 1 + src/ImageProcessor/Processors/Mask.cs | 97 ++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/ImageProcessor/Processors/Mask.cs diff --git a/src/ImageProcessor.Playground/Program.cs b/src/ImageProcessor.Playground/Program.cs index 9a1d1021f..ec95d6f19 100644 --- a/src/ImageProcessor.Playground/Program.cs +++ b/src/ImageProcessor.Playground/Program.cs @@ -61,7 +61,7 @@ namespace ImageProcessor.PlayGround { using (ImageFactory imageFactory = new ImageFactory(true)) { - Size size = new Size(800, 0); + Size size = new Size(400, 0); ResizeLayer layer = new ResizeLayer(size, ResizeMode.Max, AnchorPosition.Center, false); //ContentAwareResizeLayer layer = new ContentAwareResizeLayer(size) @@ -73,14 +73,15 @@ namespace ImageProcessor.PlayGround //.BackgroundColor(Color.White) //.Resize(new Size((int)(size.Width * 1.1), 0)) //.ContentAwareResize(layer) - // .Constrain(size) + .Constrain(size) + .Mask() //.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80) //.Resize(layer) //.DetectEdges(new SobelEdgeFilter(), false) //.DetectEdges(new LaplacianOfGaussianEdgeFilter()) //.EntropyCrop() //.Filter(MatrixFilters.Invert) - .Filter(MatrixFilters.Comic) + //.Filter(MatrixFilters.Comic) //.Filter(MatrixFilters.HiSatch) //.Pixelate(8) //.GaussianSharpen(10) diff --git a/src/ImageProcessor/ImageFactory.cs b/src/ImageProcessor/ImageFactory.cs index 060524c86..4137c4f30 100644 --- a/src/ImageProcessor/ImageFactory.cs +++ b/src/ImageProcessor/ImageFactory.cs @@ -670,6 +670,17 @@ namespace ImageProcessor return this; } + public ImageFactory Mask() + { + if (this.ShouldProcess) + { + Mask mask = new Mask(); + this.CurrentImageFormat.ApplyProcessor(mask.ProcessImage, this); + } + + return this; + } + /// /// Pixelates an image with the given size. /// diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index 5cb599083..032ccd13c 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -205,6 +205,7 @@ + diff --git a/src/ImageProcessor/Processors/Mask.cs b/src/ImageProcessor/Processors/Mask.cs new file mode 100644 index 000000000..372be395e --- /dev/null +++ b/src/ImageProcessor/Processors/Mask.cs @@ -0,0 +1,97 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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; + } + } +}