diff --git a/ShareX.HelpersLib/ColorMatrixManager.cs b/ShareX.HelpersLib/ColorMatrixManager.cs index 930e8e296..b4902e697 100644 --- a/ShareX.HelpersLib/ColorMatrixManager.cs +++ b/ShareX.HelpersLib/ColorMatrixManager.cs @@ -47,10 +47,11 @@ public static Bitmap Apply(this ColorMatrix matrix, Bitmap bmp) { Bitmap dest = bmp.CreateEmptyBitmap(); Rectangle destRect = new Rectangle(0, 0, dest.Width, dest.Height); - return Apply(matrix, bmp, dest, destRect); + Apply(matrix, bmp, dest, destRect); + return dest; } - public static Bitmap Apply(this ColorMatrix matrix, Bitmap src, Bitmap dest, Rectangle destRect) + public static void Apply(this ColorMatrix matrix, Bitmap src, Bitmap dest, Rectangle destRect) { using (Graphics g = Graphics.FromImage(dest)) using (ImageAttributes ia = new ImageAttributes()) @@ -60,8 +61,6 @@ public static Bitmap Apply(this ColorMatrix matrix, Bitmap src, Bitmap dest, Rec g.SetHighQuality(); g.DrawImage(src, destRect, 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, ia); } - - return dest; } /// @@ -238,7 +237,7 @@ public static ColorMatrix Colorize(Color color, float value) public static ColorMatrix Mask(float opacity, Color color) { return new ColorMatrix(new[] -{ + { new float[] { 0, 0, 0, 0, 0 }, new float[] { 0, 0, 0, 0, 0 }, new float[] { 0, 0, 0, 0, 0 }, diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index 1c2674672..ce4dd671f 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -1012,10 +1012,7 @@ public static Bitmap AddShadow(Bitmap bmp, float opacity, int size, float darkne if (darkness > 1) { - ColorMatrix alphaMatrix = new ColorMatrix(); - alphaMatrix.Matrix33 = darkness; - - Bitmap shadowImage2 = alphaMatrix.Apply(shadowImage); + Bitmap shadowImage2 = ColorMatrixManager.Alpha(darkness).Apply(shadowImage); shadowImage.Dispose(); shadowImage = shadowImage2; } @@ -1038,6 +1035,79 @@ public static Bitmap AddShadow(Bitmap bmp, float opacity, int size, float darkne } } + public static Bitmap AddGlow(Bitmap bmp, int size, float strength, Color color, Point offset, GradientInfo gradient = null) + { + if (size < 1 || strength < 0.1f) + { + return bmp; + } + + Bitmap glowImage = null; + + try + { + glowImage = AddCanvas(bmp, new Padding(size)); + BoxBlur(glowImage, size); + + if (gradient != null && gradient.IsValid) + { + Bitmap glowImage2 = CreateGradientMask(glowImage, gradient, strength); + glowImage.Dispose(); + glowImage = glowImage2; + } + else + { + Bitmap glowImage2 = ColorMatrixManager.Mask(strength, color).Apply(glowImage); + glowImage.Dispose(); + glowImage = glowImage2; + } + + Bitmap bmpResult = glowImage.CreateEmptyBitmap(Math.Abs(offset.X), Math.Abs(offset.Y)); + + using (Graphics g = Graphics.FromImage(bmpResult)) + { + g.SetHighQuality(); + g.DrawImage(glowImage, Math.Max(0, offset.X), Math.Max(0, offset.Y), glowImage.Width, glowImage.Height); + g.DrawImage(bmp, Math.Max(size, -offset.X + size), Math.Max(size, -offset.Y + size), bmp.Width, bmp.Height); + } + + return bmpResult; + } + finally + { + if (bmp != null) bmp.Dispose(); + if (glowImage != null) glowImage.Dispose(); + } + } + + public static Bitmap CreateGradientMask(Bitmap bmp, GradientInfo gradient, float opacity = 1f) + { + Bitmap mask = bmp.CreateEmptyBitmap(); + + if (opacity <= 0) + { + return mask; + } + + gradient.Draw(mask); + + using (UnsafeBitmap sourceBmp = new UnsafeBitmap(bmp, true)) + using (UnsafeBitmap maskBmp = new UnsafeBitmap(mask, true)) + { + int pixelCount = sourceBmp.PixelCount; + + for (int i = 0; i < pixelCount; i++) + { + ColorBgra sourceColor = sourceBmp.GetPixel(i); + ColorBgra maskColor = maskBmp.GetPixel(i); + maskColor.Alpha = (byte)Math.Min(255, sourceColor.Alpha * opacity); + maskBmp.SetPixel(i, maskColor); + } + } + + return mask; + } + public static Bitmap Sharpen(Bitmap bmp, double strength) { if (bmp != null) diff --git a/ShareX.HelpersLib/UITypeEditors/GradientEditor.cs b/ShareX.HelpersLib/UITypeEditors/GradientEditor.cs index 595e717d7..5610b9903 100644 --- a/ShareX.HelpersLib/UITypeEditors/GradientEditor.cs +++ b/ShareX.HelpersLib/UITypeEditors/GradientEditor.cs @@ -73,7 +73,10 @@ public override void PaintValue(PaintValueEventArgs e) { Graphics g = e.Graphics; GradientInfo gradient = (GradientInfo)e.Value; - gradient.Draw(g, e.Bounds); + if (gradient != null) + { + gradient.Draw(g, e.Bounds); + } g.DrawRectangleProper(Pens.Black, e.Bounds); } } diff --git a/ShareX.ImageEffectsLib/Drawings/DrawTextEx.cs b/ShareX.ImageEffectsLib/Drawings/DrawTextEx.cs index c1ea37b13..ce4343acc 100644 --- a/ShareX.ImageEffectsLib/Drawings/DrawTextEx.cs +++ b/ShareX.ImageEffectsLib/Drawings/DrawTextEx.cs @@ -110,17 +110,14 @@ public Font Font public DrawTextEx() { this.ApplyDefaultPropertyValues(); - - Gradient = new GradientInfo(); - AddDefaultGradient(Gradient); - OutlineGradient = new GradientInfo(); - AddDefaultGradient(OutlineGradient); - ShadowGradient = new GradientInfo(); - AddDefaultGradient(ShadowGradient); + Gradient = AddDefaultGradient(); + OutlineGradient = AddDefaultGradient(); + ShadowGradient = AddDefaultGradient(); } - private void AddDefaultGradient(GradientInfo gradientInfo) + private GradientInfo AddDefaultGradient() { + GradientInfo gradientInfo = new GradientInfo(); gradientInfo.Type = LinearGradientMode.Horizontal; switch (RandomFast.Next(0, 2)) @@ -138,6 +135,8 @@ private void AddDefaultGradient(GradientInfo gradientInfo) gradientInfo.Colors.Add(new GradientStop(Color.FromArgb(98, 54, 255), 100f)); break; } + + return gradientInfo; } public override Bitmap Apply(Bitmap bmp) diff --git a/ShareX.ImageEffectsLib/Filters/Glow.cs b/ShareX.ImageEffectsLib/Filters/Glow.cs new file mode 100644 index 000000000..d6f5a4eac --- /dev/null +++ b/ShareX.ImageEffectsLib/Filters/Glow.cs @@ -0,0 +1,114 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2021 ShareX Team + + 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 ShareX.HelpersLib; +using System.ComponentModel; +using System.Drawing; +using System.Drawing.Design; +using System.Drawing.Drawing2D; +using System.Linq; + +namespace ShareX.ImageEffectsLib +{ + internal class Glow : ImageEffect + { + private int size; + + [DefaultValue(20)] + public int Size + { + get + { + return size; + } + set + { + size = value.Max(1); + } + } + + private float strength; + + [DefaultValue(1f)] + public float Strength + { + get + { + return strength; + } + set + { + strength = value.Max(0.1f); + } + } + + [DefaultValue(typeof(Color), "White"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))] + public Color Color { get; set; } + + [DefaultValue(false)] + public bool UseGradient { get; set; } + + [Editor(typeof(GradientEditor), typeof(UITypeEditor))] + public GradientInfo Gradient { get; set; } + + [DefaultValue(typeof(Point), "0, 0")] + public Point Offset { get; set; } + + public Glow() + { + this.ApplyDefaultPropertyValues(); + Gradient = AddDefaultGradient(); + } + + private GradientInfo AddDefaultGradient() + { + GradientInfo gradientInfo = new GradientInfo(); + gradientInfo.Type = LinearGradientMode.ForwardDiagonal; + + switch (RandomFast.Next(0, 2)) + { + case 0: + gradientInfo.Colors.Add(new GradientStop(Color.FromArgb(0, 187, 138), 0f)); + gradientInfo.Colors.Add(new GradientStop(Color.FromArgb(0, 105, 163), 100f)); + break; + case 1: + gradientInfo.Colors.Add(new GradientStop(Color.FromArgb(255, 3, 135), 0f)); + gradientInfo.Colors.Add(new GradientStop(Color.FromArgb(255, 143, 3), 100f)); + break; + case 2: + gradientInfo.Colors.Add(new GradientStop(Color.FromArgb(184, 11, 195), 0f)); + gradientInfo.Colors.Add(new GradientStop(Color.FromArgb(98, 54, 255), 100f)); + break; + } + + return gradientInfo; + } + + public override Bitmap Apply(Bitmap bmp) + { + return ImageHelpers.AddGlow(bmp, Size, Strength, Color, Offset, UseGradient ? Gradient : null); + } + } +} \ No newline at end of file diff --git a/ShareX.ImageEffectsLib/Forms/ImageEffectsForm.cs b/ShareX.ImageEffectsLib/Forms/ImageEffectsForm.cs index b0597f6b8..e2a717b46 100644 --- a/ShareX.ImageEffectsLib/Forms/ImageEffectsForm.cs +++ b/ShareX.ImageEffectsLib/Forms/ImageEffectsForm.cs @@ -191,6 +191,7 @@ private void AddAllEffectsToContextMenu() typeof(EdgeDetect), typeof(Emboss), typeof(GaussianBlur), + typeof(Glow), typeof(MeanRemoval), typeof(Outline), typeof(Pixelate), diff --git a/ShareX.ImageEffectsLib/ShareX.ImageEffectsLib.csproj b/ShareX.ImageEffectsLib/ShareX.ImageEffectsLib.csproj index 131070fc3..3bb5d6bbc 100644 --- a/ShareX.ImageEffectsLib/ShareX.ImageEffectsLib.csproj +++ b/ShareX.ImageEffectsLib/ShareX.ImageEffectsLib.csproj @@ -118,6 +118,7 @@ +