From 019c9a7dae8dfab84fa2042b04945d7156c414e0 Mon Sep 17 00:00:00 2001 From: Jaex Date: Sat, 20 Apr 2024 09:16:12 +0300 Subject: [PATCH] Code refactoring --- ShareX.HelpersLib/Helpers/ImageHelpers.cs | 49 ++++++++++++------- ShareX.ImageEffectsLib/Filters/TornEdge.cs | 6 +-- ShareX.ImageEffectsLib/Filters/WaveEdge.cs | 6 +-- .../Properties/Resources.Designer.cs | 2 +- .../Properties/Resources.resx | 2 +- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index 14406ffb2..9a18475ef 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -260,24 +260,24 @@ namespace ShareX.HelpersLib return null; } - private static Bitmap ApplyCutOutEffect(Bitmap bmp, AnchorStyles effectEdge, CutOutEffectType effectType, int effectSize, Color cutOutBackgroundColor) + private static Bitmap ApplyCutOutEffect(Bitmap bmp, AnchorStyles effectEdge, CutOutEffectType effectType, int effectSize, Color backgroundColor) { switch (effectType) { case CutOutEffectType.None: return bmp; case CutOutEffectType.ZigZag: - return TornEdges(bmp, effectSize, effectSize, effectEdge, false, false, cutOutBackgroundColor); + return TornEdges(bmp, effectSize, effectSize, effectEdge, false, false, backgroundColor); case CutOutEffectType.TornEdge: - return TornEdges(bmp, effectSize, effectSize * 2, effectEdge, false, true, cutOutBackgroundColor); + return TornEdges(bmp, effectSize, effectSize * 2, effectEdge, false, true, backgroundColor); case CutOutEffectType.Wave: - return WavyEdges(bmp, effectSize, effectSize * 5, effectEdge, cutOutBackgroundColor); + return WavyEdges(bmp, effectSize, effectSize * 5, effectEdge, backgroundColor); } throw new NotImplementedException(); } - public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int start, int size, CutOutEffectType effectType, int effectSize, Color cutOutBackgroundColor) + public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int start, int size, CutOutEffectType effectType, int effectSize, Color backgroundColor) { if (bmp != null && size > 0) { @@ -290,7 +290,7 @@ namespace ShareX.HelpersLib : new Rectangle(0, 0, bmp.Width, Math.Min(start, bmp.Height)); firstPart = CropBitmap(bmp, r); AnchorStyles effectEdge = orientation == Orientation.Horizontal ? AnchorStyles.Right : AnchorStyles.Bottom; - firstPart = ApplyCutOutEffect(firstPart, effectEdge, effectType, effectSize, cutOutBackgroundColor); + firstPart = ApplyCutOutEffect(firstPart, effectEdge, effectType, effectSize, backgroundColor); } int cutDimension = orientation == Orientation.Horizontal ? bmp.Width : bmp.Height; @@ -302,7 +302,7 @@ namespace ShareX.HelpersLib : new Rectangle(0, end, bmp.Width, bmp.Height - end); secondPart = CropBitmap(bmp, r); AnchorStyles effectEdge = orientation == Orientation.Horizontal ? AnchorStyles.Left : AnchorStyles.Top; - secondPart = ApplyCutOutEffect(secondPart, effectEdge, effectType, effectSize, cutOutBackgroundColor); + secondPart = ApplyCutOutEffect(secondPart, effectEdge, effectType, effectSize, backgroundColor); } if (firstPart != null && secondPart != null) @@ -1844,7 +1844,12 @@ namespace ShareX.HelpersLib } } - public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorStyles sides, Color cutOutBackgroundColor) + public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorStyles sides) + { + return WavyEdges(bmp, waveDepth, waveRange, sides, Color.Transparent); + } + + public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorStyles sides, Color backgroundColor) { if (waveDepth < 1 || waveRange < 1 || sides == AnchorStyles.None) { @@ -1928,22 +1933,31 @@ namespace ShareX.HelpersLib } Bitmap bmpResult = bmp.CreateEmptyBitmap(); + using (bmp) using (Graphics g = Graphics.FromImage(bmpResult)) using (TextureBrush brush = new TextureBrush(bmp)) { + if (backgroundColor.A > 0) + { + g.Clear(backgroundColor); + } + g.SetHighQuality(); g.PixelOffsetMode = PixelOffsetMode.Half; - if (cutOutBackgroundColor.A > 0) - { - g.Clear(cutOutBackgroundColor); - } + g.FillPolygon(brush, points.ToArray()); } + return bmpResult; } - public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random, Color cutOutBackgroundColor) + public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random) + { + return TornEdges(bmp, tornDepth, tornRange, sides, curvedEdges, random, Color.Transparent); + } + + public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random, Color backgroundColor) { if (tornDepth < 1 || tornRange < 1 || sides == AnchorStyles.None) { @@ -2030,12 +2044,13 @@ namespace ShareX.HelpersLib using (Graphics g = Graphics.FromImage(bmpResult)) using (TextureBrush brush = new TextureBrush(bmp)) { + if (backgroundColor.A > 0) + { + g.Clear(backgroundColor); + } + g.SetHighQuality(); g.PixelOffsetMode = PixelOffsetMode.Half; - if (cutOutBackgroundColor.A > 0) - { - g.Clear(cutOutBackgroundColor); - } Point[] fillPoints = points.Distinct().ToArray(); diff --git a/ShareX.ImageEffectsLib/Filters/TornEdge.cs b/ShareX.ImageEffectsLib/Filters/TornEdge.cs index d04d80d7f..65fbd6b4a 100644 --- a/ShareX.ImageEffectsLib/Filters/TornEdge.cs +++ b/ShareX.ImageEffectsLib/Filters/TornEdge.cs @@ -26,7 +26,6 @@ using ShareX.HelpersLib; using System.ComponentModel; using System.Drawing; -using System.Drawing.Design; using System.Windows.Forms; namespace ShareX.ImageEffectsLib @@ -46,9 +45,6 @@ namespace ShareX.ImageEffectsLib [DefaultValue(true)] public bool CurvedEdges { get; set; } - [DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))] - public Color CutOutBackgroundColor { get; set; } - public TornEdge() { this.ApplyDefaultPropertyValues(); @@ -56,7 +52,7 @@ namespace ShareX.ImageEffectsLib public override Bitmap Apply(Bitmap bmp) { - return ImageHelpers.TornEdges(bmp, Depth, Range, Sides, CurvedEdges, true, CutOutBackgroundColor); + return ImageHelpers.TornEdges(bmp, Depth, Range, Sides, CurvedEdges, true); } protected override string GetSummary() diff --git a/ShareX.ImageEffectsLib/Filters/WaveEdge.cs b/ShareX.ImageEffectsLib/Filters/WaveEdge.cs index 3dc0b819b..2f94e5a55 100644 --- a/ShareX.ImageEffectsLib/Filters/WaveEdge.cs +++ b/ShareX.ImageEffectsLib/Filters/WaveEdge.cs @@ -26,7 +26,6 @@ using ShareX.HelpersLib; using System.ComponentModel; using System.Drawing; -using System.Drawing.Design; using System.Windows.Forms; namespace ShareX.ImageEffectsLib @@ -48,12 +47,9 @@ namespace ShareX.ImageEffectsLib this.ApplyDefaultPropertyValues(); } - [DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))] - public Color CutOutBackgroundColor { get; set; } - public override Bitmap Apply(Bitmap bmp) { - return ImageHelpers.WavyEdges(bmp, Depth, Range, Sides, CutOutBackgroundColor); + return ImageHelpers.WavyEdges(bmp, Depth, Range, Sides); } protected override string GetSummary() diff --git a/ShareX.ScreenCaptureLib/Properties/Resources.Designer.cs b/ShareX.ScreenCaptureLib/Properties/Resources.Designer.cs index b3c070c65..e3e2c4013 100644 --- a/ShareX.ScreenCaptureLib/Properties/Resources.Designer.cs +++ b/ShareX.ScreenCaptureLib/Properties/Resources.Designer.cs @@ -324,7 +324,7 @@ namespace ShareX.ScreenCaptureLib.Properties { } /// - /// Looks up a localized string similar to Cut out background color. + /// Looks up a localized string similar to Cut out background color.... /// internal static string CutOutBackgroundColor { get { diff --git a/ShareX.ScreenCaptureLib/Properties/Resources.resx b/ShareX.ScreenCaptureLib/Properties/Resources.resx index 070e808e4..fab315bb7 100644 --- a/ShareX.ScreenCaptureLib/Properties/Resources.resx +++ b/ShareX.ScreenCaptureLib/Properties/Resources.resx @@ -831,6 +831,6 @@ Would you like to save the changes before closing the image editor? ..\Resources\control-record-green.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - Cut out background color + Cut out background color... \ No newline at end of file