From 5aefb10de4b58eaa7a11f1610c67135b2c2cea14 Mon Sep 17 00:00:00 2001 From: Jaex Date: Thu, 16 Nov 2017 16:00:11 +0300 Subject: [PATCH] Refactor effect shapes --- .../Shapes/Effect/BaseEffectShape.cs | 39 +++++++++++++++++- .../Shapes/Effect/BlurEffectShape.cs | 38 ++--------------- .../Shapes/Effect/HighlightEffectShape.cs | 41 +------------------ .../Shapes/Effect/PixelateEffectShape.cs | 38 ++--------------- 4 files changed, 45 insertions(+), 111 deletions(-) diff --git a/ShareX.ScreenCaptureLib/Shapes/Effect/BaseEffectShape.cs b/ShareX.ScreenCaptureLib/Shapes/Effect/BaseEffectShape.cs index 2bb2b8ff9..91992877a 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Effect/BaseEffectShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Effect/BaseEffectShape.cs @@ -23,6 +23,7 @@ You should have received a copy of the GNU General Public License #endregion License Information (GPL v3) +using ShareX.HelpersLib; using System.Drawing; using System.Drawing.Drawing2D; @@ -32,6 +33,8 @@ public abstract class BaseEffectShape : BaseShape { public override ShapeCategory ShapeCategory { get; } = ShapeCategory.Effect; + public abstract string OverlayText { get; } + private Image cachedEffect; public abstract void ApplyEffect(Bitmap bmp); @@ -50,11 +53,43 @@ public virtual void OnDraw(Graphics g) } } - public abstract void OnDrawOverlay(Graphics g); + public virtual void OnDrawOverlay(Graphics g) + { + using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black))) + { + g.FillRectangle(brush, Rectangle); + } + + g.DrawCornerLines(Rectangle.Offset(1), Pens.White, 25); + + using (Font font = new Font("Verdana", 12)) + { + string text = OverlayText; + Size textSize = g.MeasureString(text, font).ToSize(); + + if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height) + { + using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) + { + g.DrawString(text, font, Brushes.White, Rectangle, sf); + } + } + } + } public virtual void OnDrawFinal(Graphics g, Bitmap bmp) { - OnDraw(g); + Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle); + + if (!rect.IsEmpty) + { + using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect)) + { + ApplyEffect(croppedImage); + + g.DrawImage(croppedImage, rect); + } + } } public override void OnCreated() diff --git a/ShareX.ScreenCaptureLib/Shapes/Effect/BlurEffectShape.cs b/ShareX.ScreenCaptureLib/Shapes/Effect/BlurEffectShape.cs index ee6883b0f..24640aadd 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Effect/BlurEffectShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Effect/BlurEffectShape.cs @@ -32,6 +32,8 @@ public class BlurEffectShape : BaseEffectShape { public override ShapeType ShapeType { get; } = ShapeType.EffectBlur; + public override string OverlayText => $"Blur [{BlurRadius}]"; + public int BlurRadius { get; set; } public override void OnConfigLoad() @@ -49,45 +51,11 @@ public override void ApplyEffect(Bitmap bmp) ImageHelpers.BoxBlur(bmp, BlurRadius); } - public override void OnDrawOverlay(Graphics g) - { - using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black))) - { - g.FillRectangle(brush, Rectangle); - } - - g.DrawCornerLines(Rectangle.Offset(1), Pens.White, 20); - - using (Font font = new Font("Verdana", 12)) - { - string text = $"Blur ({BlurRadius})"; - Size textSize = g.MeasureString(text, font).ToSize(); - - if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height) - { - using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) - { - g.DrawString(text, font, Brushes.White, Rectangle, sf); - } - } - } - } - public override void OnDrawFinal(Graphics g, Bitmap bmp) { if (BlurRadius > 1) { - Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle); - - if (!rect.IsEmpty) - { - using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect)) - { - ApplyEffect(croppedImage); - - g.DrawImage(croppedImage, rect); - } - } + base.OnDrawFinal(g, bmp); } } } diff --git a/ShareX.ScreenCaptureLib/Shapes/Effect/HighlightEffectShape.cs b/ShareX.ScreenCaptureLib/Shapes/Effect/HighlightEffectShape.cs index 4f6212cfd..fba48e794 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Effect/HighlightEffectShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Effect/HighlightEffectShape.cs @@ -32,6 +32,8 @@ public class HighlightEffectShape : BaseEffectShape { public override ShapeType ShapeType { get; } = ShapeType.EffectHighlight; + public override string OverlayText => "Highlight"; + public Color HighlightColor { get; set; } public override void OnConfigLoad() @@ -48,44 +50,5 @@ public override void ApplyEffect(Bitmap bmp) { ImageHelpers.HighlightImage(bmp, HighlightColor); } - - public override void OnDrawOverlay(Graphics g) - { - using (Brush brush = new SolidBrush(Color.FromArgb(100, HighlightColor))) - { - g.FillRectangle(brush, Rectangle); - } - - g.DrawCornerLines(Rectangle.Offset(1), Pens.Black, 20); - - using (Font font = new Font("Verdana", 12)) - { - string text = "Highlight"; - Size textSize = g.MeasureString(text, font).ToSize(); - - if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height) - { - using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) - { - g.DrawString(text, font, Brushes.Black, Rectangle, sf); - } - } - } - } - - public override void OnDrawFinal(Graphics g, Bitmap bmp) - { - Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle); - - if (!rect.IsEmpty) - { - using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect)) - { - ApplyEffect(croppedImage); - - g.DrawImage(croppedImage, rect); - } - } - } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Shapes/Effect/PixelateEffectShape.cs b/ShareX.ScreenCaptureLib/Shapes/Effect/PixelateEffectShape.cs index 9edb6048d..cd79b7b3a 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Effect/PixelateEffectShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Effect/PixelateEffectShape.cs @@ -32,6 +32,8 @@ public class PixelateEffectShape : BaseEffectShape { public override ShapeType ShapeType { get; } = ShapeType.EffectPixelate; + public override string OverlayText => $"Pixelate [{PixelSize}]"; + public int PixelSize { get; set; } public override void OnConfigLoad() @@ -49,45 +51,11 @@ public override void ApplyEffect(Bitmap bmp) ImageHelpers.Pixelate(bmp, PixelSize); } - public override void OnDrawOverlay(Graphics g) - { - using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black))) - { - g.FillRectangle(brush, Rectangle); - } - - g.DrawCornerLines(Rectangle.Offset(1), Pens.White, 20); - - using (Font font = new Font("Verdana", 12)) - { - string text = $"Pixelate ({PixelSize})"; - Size textSize = g.MeasureString(text, font).ToSize(); - - if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height) - { - using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) - { - g.DrawString(text, font, Brushes.White, Rectangle, sf); - } - } - } - } - public override void OnDrawFinal(Graphics g, Bitmap bmp) { if (PixelSize > 1) { - Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle); - - if (!rect.IsEmpty) - { - using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect)) - { - ApplyEffect(croppedImage); - - g.DrawImage(croppedImage, rect); - } - } + base.OnDrawFinal(g, bmp); } } }