From 1591951c0803d36d0873d7e0f00b86b5a262679f Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 24 Apr 2023 13:45:22 +0300 Subject: [PATCH] Added "Padding" option to "Auto crop" image effect --- ShareX.HelpersLib/Helpers/ImageHelpers.cs | 22 ++++++++++++++++--- .../Manipulations/AutoCrop.cs | 5 ++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index e6933af08..8820b7603 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -452,6 +452,11 @@ public static Bitmap AddCanvas(Image img, int margin) return AddCanvas(img, new Padding(margin)); } + public static Bitmap AddCanvas(Image img, int margin, Color canvasColor) + { + return AddCanvas(img, new Padding(margin), canvasColor); + } + public static Bitmap AddCanvas(Image img, Padding margin) { return AddCanvas(img, margin, Color.Transparent); @@ -2464,7 +2469,7 @@ public static void DrawColorPickerIcon(Graphics g, Color color, Rectangle rect, } public static Bitmap AutoCropImage(Bitmap bmp, bool sameColorCrop = false, - AnchorStyles sides = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right) + AnchorStyles sides = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right, int padding = 0) { Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height); Rectangle rect = FindAutoCropRectangle(bmp, sameColorCrop, sides); @@ -2475,8 +2480,19 @@ public static void DrawColorPickerIcon(Graphics g, Color color, Rectangle rect, if (croppedBitmap != null) { - bmp.Dispose(); - return croppedBitmap; + using (bmp) + { + if (padding > 0) + { + using (croppedBitmap) + { + Color color = bmp.GetPixel(0, 0); + return AddCanvas(croppedBitmap, padding, color); + } + } + + return croppedBitmap; + } } } diff --git a/ShareX.ImageEffectsLib/Manipulations/AutoCrop.cs b/ShareX.ImageEffectsLib/Manipulations/AutoCrop.cs index 1732d891d..6b6c529f9 100644 --- a/ShareX.ImageEffectsLib/Manipulations/AutoCrop.cs +++ b/ShareX.ImageEffectsLib/Manipulations/AutoCrop.cs @@ -36,6 +36,9 @@ internal class AutoCrop : ImageEffect [DefaultValue(AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right)] public AnchorStyles Sides { get; set; } + [DefaultValue(0)] + public int Padding { get; set; } + public AutoCrop() { this.ApplyDefaultPropertyValues(); @@ -43,7 +46,7 @@ public AutoCrop() public override Bitmap Apply(Bitmap bmp) { - return ImageHelpers.AutoCropImage(bmp, false, Sides); + return ImageHelpers.AutoCropImage(bmp, true, Sides, Padding); } } } \ No newline at end of file