// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Encapsulates methods with which to add a vignette image effect to an image. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Processors { using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using ImageProcessor.Core.Common.Exceptions; /// /// Encapsulates methods with which to add a vignette image effect to an image. /// public class Vignette : IGraphicsProcessor { /// /// Initializes a new instance of the class. /// public Vignette() { this.DynamicParameter = Color.Black; 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 { newImage = new Bitmap(image); using (Graphics graphics = Graphics.FromImage(newImage)) { Rectangle bounds = new Rectangle(0, 0, newImage.Width, newImage.Height); Rectangle ellipsebounds = bounds; // Increase the rectangle size by the difference between the rectangle dimensions and sqrt(2)/2 * the rectangle dimensions. // Why sqrt(2)/2? Because the point (sqrt(2)/2, sqrt(2)/2) is the 45 degree angle point on a unit circle. Scaling by the width // and height gives the distance needed to inflate the rect to make sure it's fully covered. ellipsebounds.Offset(-ellipsebounds.X, -ellipsebounds.Y); int x = ellipsebounds.Width - (int)Math.Floor(.70712 * ellipsebounds.Width); int y = ellipsebounds.Height - (int)Math.Floor(.70712 * ellipsebounds.Height); ellipsebounds.Inflate(x, y); using (GraphicsPath path = new GraphicsPath()) { path.AddEllipse(ellipsebounds); using (PathGradientBrush brush = new PathGradientBrush(path)) { // Fill a rectangle with an elliptical gradient brush that goes from transparent to opaque. // This has the effect of painting the far corners with the given color and shade less on the way in to the centre. Color baseColor = (Color)this.DynamicParameter; Color centerColor = Color.FromArgb(0, baseColor.R, baseColor.G, baseColor.B); Color edgeColor = Color.FromArgb(255, baseColor.R, baseColor.G, baseColor.B); brush.WrapMode = WrapMode.Tile; brush.CenterColor = centerColor; brush.SurroundColors = new[] { edgeColor }; Blend blend = new Blend { Positions = new[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0F }, Factors = new[] { 0.0f, 0.5f, 1f, 1f, 1.0f, 1.0f } }; brush.Blend = blend; Region oldClip = graphics.Clip; graphics.Clip = new Region(bounds); graphics.FillRectangle(brush, ellipsebounds); graphics.Clip = oldClip; } // 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; } } }