From 3f13e0f025da0f8d4837e3665929f0dfda58a352 Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 20 Oct 2014 22:37:51 +0300 Subject: [PATCH] Added Outline image effect --- HelpersLib/Helpers/ImageHelpers.cs | 31 +++++++++++ ImageEffectsLib/Filters/Outline.cs | 63 +++++++++++++++++++++++ ImageEffectsLib/ImageEffectsForm.cs | 1 + ImageEffectsLib/ImageEffectsLib.csproj | 1 + ScreenCaptureLib/Forms/RectangleRegion.cs | 2 +- 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 ImageEffectsLib/Filters/Outline.cs diff --git a/HelpersLib/Helpers/ImageHelpers.cs b/HelpersLib/Helpers/ImageHelpers.cs index 950f8934d..368be3483 100644 --- a/HelpersLib/Helpers/ImageHelpers.cs +++ b/HelpersLib/Helpers/ImageHelpers.cs @@ -315,6 +315,37 @@ public static Image RoundedCorners(Image img, int cornerRadius) return bmp; } + public static Image Outline(Image img, int borderSize, Color borderColor) + { + Bitmap result = img.CreateEmptyBitmap(borderSize * 2, borderSize * 2); + + ColorMatrix maskMatrix = new ColorMatrix(); + maskMatrix.Matrix00 = 0; + maskMatrix.Matrix11 = 0; + maskMatrix.Matrix22 = 0; + maskMatrix.Matrix33 = 1; + maskMatrix.Matrix40 = ((float)borderColor.R).Remap(0, 255, 0, 1); + maskMatrix.Matrix41 = ((float)borderColor.G).Remap(0, 255, 0, 1); + maskMatrix.Matrix42 = ((float)borderColor.B).Remap(0, 255, 0, 1); + + using (img) + using (Image shadow = maskMatrix.Apply(img)) + using (Graphics g = Graphics.FromImage(result)) + { + for (int i = 0; i <= borderSize * 2; i++) + { + g.DrawImage(shadow, new Rectangle(i, 0, shadow.Width, shadow.Height)); + g.DrawImage(shadow, new Rectangle(i, borderSize * 2, shadow.Width, shadow.Height)); + g.DrawImage(shadow, new Rectangle(0, i, shadow.Width, shadow.Height)); + g.DrawImage(shadow, new Rectangle(borderSize * 2, i, shadow.Width, shadow.Height)); + } + + g.DrawImage(img, new Rectangle(borderSize, borderSize, img.Width, img.Height)); + } + + return result; + } + public static Image DrawReflection(Image img, int percentage, int maxAlpha, int minAlpha, int offset, bool skew, int skewSize) { Bitmap reflection = AddReflection(img, percentage, maxAlpha, minAlpha); diff --git a/ImageEffectsLib/Filters/Outline.cs b/ImageEffectsLib/Filters/Outline.cs new file mode 100644 index 000000000..d14818384 --- /dev/null +++ b/ImageEffectsLib/Filters/Outline.cs @@ -0,0 +1,63 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (C) 2007-2014 ShareX Developers + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using HelpersLib; +using System.ComponentModel; +using System.Drawing; +using System.Drawing.Design; + +namespace ImageEffectsLib +{ + internal class Outline : ImageEffect + { + private int size; + + [DefaultValue(1)] + public int Size + { + get + { + return size; + } + set + { + size = value.Min(1); + } + } + + [DefaultValue(typeof(Color), "Black"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))] + public Color Color { get; set; } + + public Outline() + { + this.ApplyDefaultPropertyValues(); + } + + public override Image Apply(Image img) + { + return ImageHelpers.Outline(img, Size, Color); + } + } +} \ No newline at end of file diff --git a/ImageEffectsLib/ImageEffectsForm.cs b/ImageEffectsLib/ImageEffectsForm.cs index 1b81d07ca..c1745855e 100644 --- a/ImageEffectsLib/ImageEffectsForm.cs +++ b/ImageEffectsLib/ImageEffectsForm.cs @@ -104,6 +104,7 @@ private void AddAllEffectsToTreeView() typeof(GaussianBlur), typeof(MatrixConvolution), typeof(MeanRemoval), + typeof(Outline), typeof(Pixelate), typeof(Reflection), typeof(Shadow), diff --git a/ImageEffectsLib/ImageEffectsLib.csproj b/ImageEffectsLib/ImageEffectsLib.csproj index a7260f634..e2e7b5acc 100644 --- a/ImageEffectsLib/ImageEffectsLib.csproj +++ b/ImageEffectsLib/ImageEffectsLib.csproj @@ -80,6 +80,7 @@ + diff --git a/ScreenCaptureLib/Forms/RectangleRegion.cs b/ScreenCaptureLib/Forms/RectangleRegion.cs index d4c04d86e..69d308bf7 100644 --- a/ScreenCaptureLib/Forms/RectangleRegion.cs +++ b/ScreenCaptureLib/Forms/RectangleRegion.cs @@ -420,4 +420,4 @@ protected virtual void AddShapePath(GraphicsPath graphicsPath, Rectangle rect) graphicsPath.AddRectangle(rect); } } -} +} \ No newline at end of file