Added sides option to auto crop image effect

This commit is contained in:
Jaex 2018-06-14 14:12:19 +03:00
parent 6c696fd034
commit 599cfc2a0e
2 changed files with 77 additions and 46 deletions

View file

@ -1711,9 +1711,16 @@ public static void DrawColorPickerIcon(Graphics g, Color color, Rectangle rect,
}
}
public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = false)
public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = false,
AnchorStyles sides = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right)
{
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
if (sides == AnchorStyles.None)
{
return source;
}
Rectangle crop = source;
using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
@ -1724,6 +1731,8 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
uint mask = checkColor.Alpha == 0 ? 0xFF000000 : 0xFFFFFFFF;
uint check = checkColor.Bgra & mask;
if (sides.HasFlag(AnchorStyles.Left))
{
// Find X (Left to right)
for (int x = 0; x < bmp.Width && !leave; x++)
{
@ -1732,6 +1741,7 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.X = x;
crop.Width -= x;
leave = true;
break;
}
@ -1745,7 +1755,10 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
}
leave = false;
}
if (sides.HasFlag(AnchorStyles.Top))
{
// Find Y (Top to bottom)
for (int y = 0; y < bmp.Height && !leave; y++)
{
@ -1754,6 +1767,7 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.Y = y;
crop.Height -= y;
leave = true;
break;
}
@ -1761,6 +1775,7 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
}
leave = false;
}
if (!sameColorCrop)
{
@ -1769,6 +1784,8 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
check = checkColor.Bgra & mask;
}
if (sides.HasFlag(AnchorStyles.Right))
{
// Find Width (Right to left)
for (int x = bmp.Width - 1; x >= 0 && !leave; x--)
{
@ -1784,7 +1801,10 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
}
leave = false;
}
if (sides.HasFlag(AnchorStyles.Bottom))
{
// Find Height (Bottom to top)
for (int y = bmp.Height - 1; y >= 0 && !leave; y--)
{
@ -1799,14 +1819,16 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
}
}
}
}
return crop;
}
public static Bitmap AutoCropImage(Bitmap bmp, bool sameColorCrop = false)
public static Bitmap AutoCropImage(Bitmap bmp, bool sameColorCrop = false,
AnchorStyles sides = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right)
{
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
Rectangle rect = FindAutoCropRectangle(bmp, sameColorCrop);
Rectangle rect = FindAutoCropRectangle(bmp, sameColorCrop, sides);
if (source != rect)
{

View file

@ -26,15 +26,24 @@
using ShareX.HelpersLib;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ShareX.ImageEffectsLib
{
[Description("Auto crop")]
internal class AutoCrop : ImageEffect
{
[DefaultValue(AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right)]
public AnchorStyles Sides { get; set; }
public AutoCrop()
{
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
{
return ImageHelpers.AutoCropImage((Bitmap)img);
return ImageHelpers.AutoCropImage((Bitmap)img, false, Sides);
}
}
}