From 9de1953e6d686f4a8a36e3c58ca9e39b0a0c64dd Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Wed, 17 Aug 2022 16:13:55 +0200 Subject: [PATCH] Refactor CutOutBitmapMiddle --- ShareX.HelpersLib/Helpers/ImageHelpers.cs | 144 ++++++------------ .../Shapes/ShapeManager.cs | 4 +- 2 files changed, 48 insertions(+), 100 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index 6a2b78798..403785b27 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -223,124 +223,72 @@ public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect) return null; } - public static Bitmap CutOutBitmapMiddleHorizontal(Bitmap bmp, int x, int width, CutOutEffectType effectType, int effectSize) + private static Bitmap ApplyCutOutEffect(Bitmap bmp, AnchorStyles effectEdge, CutOutEffectType effectType, int effectSize) { - if (bmp != null && width > 0) + switch (effectType) { - Bitmap leftPart = null, rightPart = null; - if (x > 0) - { - leftPart = CropBitmap(bmp, new Rectangle(0, 0, Math.Min(x, bmp.Width), bmp.Height)); - switch (effectType) - { - case CutOutEffectType.None: - break; - case CutOutEffectType.ZigZag: - break; - case CutOutEffectType.TornEdge: - leftPart = TornEdges(leftPart, effectSize, effectSize * 2, AnchorStyles.Right, false); - break; - case CutOutEffectType.Wave: - break; - case CutOutEffectType.Gradient: - break; - } - } - if (x + width < bmp.Width) - { - int x2 = Math.Max(x + width, 0); - rightPart = CropBitmap(bmp, new Rectangle(x2, 0, bmp.Width - x2, bmp.Height)); - switch (effectType) - { - case CutOutEffectType.None: - break; - case CutOutEffectType.ZigZag: - break; - case CutOutEffectType.TornEdge: - rightPart = TornEdges(rightPart, effectSize, effectSize * 2, AnchorStyles.Left, false); - break; - case CutOutEffectType.Wave: - break; - case CutOutEffectType.Gradient: - break; - } - } + case CutOutEffectType.None: + return bmp; - if (leftPart != null && rightPart != null) - { - return CombineImages(new List { leftPart, rightPart }, Orientation.Horizontal); - } - else if (leftPart != null) - { - return leftPart; - } - else if (rightPart != null) - { - return rightPart; - } + case CutOutEffectType.ZigZag: + return bmp; + + case CutOutEffectType.TornEdge: + return TornEdges(bmp, effectSize, effectSize * 2, effectEdge, false); + + case CutOutEffectType.Wave: + return bmp; + + case CutOutEffectType.Gradient: + return bmp; } - return null; + throw new NotImplementedException(); // should not be reachable } - public static Bitmap CutOutBitmapMiddleVertical(Bitmap bmp, int y, int height, CutOutEffectType effectType, int effectSize) + public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int start, int size, CutOutEffectType effectType, int effectSize) { - if (bmp != null && height > 0) + if (bmp != null && size > 0) { - Bitmap topPart = null, bottomPart = null; - if (y > 0) + Bitmap firstPart = null, secondPart = null; + + if (start > 0) { - topPart = CropBitmap(bmp, new Rectangle(0, 0, bmp.Width, Math.Min(y, bmp.Height))); - switch (effectType) - { - case CutOutEffectType.None: - break; - case CutOutEffectType.ZigZag: - break; - case CutOutEffectType.TornEdge: - topPart = TornEdges(topPart, effectSize, effectSize * 2, AnchorStyles.Bottom, false); - break; - case CutOutEffectType.Wave: - break; - case CutOutEffectType.Gradient: - break; - } - } - if (y + height < bmp.Height) - { - int y2 = Math.Max(y + height, 0); - bottomPart = CropBitmap(bmp, new Rectangle(0, y2, bmp.Width, bmp.Height - y2)); - switch (effectType) - { - case CutOutEffectType.None: - break; - case CutOutEffectType.ZigZag: - break; - case CutOutEffectType.TornEdge: - bottomPart = TornEdges(bottomPart, effectSize, effectSize * 2, AnchorStyles.Top, false); - break; - case CutOutEffectType.Wave: - break; - case CutOutEffectType.Gradient: - break; - } + Rectangle r = orientation == Orientation.Horizontal + ? new Rectangle(0, 0, Math.Min(start, bmp.Width), bmp.Height) + : 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); } - if (topPart != null && bottomPart != null) + int cutDimension = orientation == Orientation.Horizontal ? bmp.Width : bmp.Height; + if (start + size < cutDimension) { - return CombineImages(new List { topPart, bottomPart }, Orientation.Vertical); + int end = Math.Max(start + size, 0); + Rectangle r = orientation == Orientation.Horizontal + ? new Rectangle(end, 0, bmp.Width - end, bmp.Height) + : 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); } - else if (topPart != null) + + if (firstPart != null && secondPart != null) { - return topPart; + return CombineImages(new List { firstPart, secondPart }, orientation); } - else if (bottomPart != null) + else if (firstPart != null) { - return bottomPart; + return firstPart; + } + else if (secondPart != null) + { + return secondPart; } } - return null; + return bmp; } /// Automatically crop image to remove transparent outside area. diff --git a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs index 168c469a1..9199912f8 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs @@ -1929,12 +1929,12 @@ public void CutOut(RectangleF rect) if (isHorizontal && cropRect.Width > 0) { CollapseAllHorizontal(rect.X, rect.Width); - UpdateCanvas(ImageHelpers.CutOutBitmapMiddleHorizontal(Form.Canvas, cropRect.X, cropRect.Width, CutOutEffectType.TornEdge, 5)); + UpdateCanvas(ImageHelpers.CutOutBitmapMiddle(Form.Canvas, Orientation.Horizontal, cropRect.X, cropRect.Width, CutOutEffectType.TornEdge, 5)); } else if (!isHorizontal && cropRect.Height > 0) { CollapseAllVertical(rect.Y, rect.Height); - UpdateCanvas(ImageHelpers.CutOutBitmapMiddleVertical(Form.Canvas, cropRect.Y, cropRect.Height, CutOutEffectType.TornEdge, 5)); + UpdateCanvas(ImageHelpers.CutOutBitmapMiddle(Form.Canvas, Orientation.Vertical, cropRect.Y, cropRect.Height, CutOutEffectType.TornEdge, 5)); } }