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,43 +1731,51 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
uint mask = checkColor.Alpha == 0 ? 0xFF000000 : 0xFFFFFFFF;
uint check = checkColor.Bgra & mask;
// Find X (Left to right)
for (int x = 0; x < bmp.Width && !leave; x++)
if (sides.HasFlag(AnchorStyles.Left))
{
for (int y = 0; y < bmp.Height; y++)
// Find X (Left to right)
for (int x = 0; x < bmp.Width && !leave; x++)
{
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
for (int y = 0; y < bmp.Height; y++)
{
crop.X = x;
leave = true;
break;
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.X = x;
crop.Width -= x;
leave = true;
break;
}
}
}
}
// If all pixels same color
if (!leave)
{
return crop;
}
leave = false;
// Find Y (Top to bottom)
for (int y = 0; y < bmp.Height && !leave; y++)
{
for (int x = 0; x < bmp.Width; x++)
// If all pixels same color
if (!leave)
{
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
return crop;
}
leave = false;
}
if (sides.HasFlag(AnchorStyles.Top))
{
// Find Y (Top to bottom)
for (int y = 0; y < bmp.Height && !leave; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
crop.Y = y;
leave = true;
break;
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.Y = y;
crop.Height -= y;
leave = true;
break;
}
}
}
}
leave = false;
leave = false;
}
if (!sameColorCrop)
{
@ -1769,32 +1784,38 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
check = checkColor.Bgra & mask;
}
// Find Width (Right to left)
for (int x = bmp.Width - 1; x >= 0 && !leave; x--)
if (sides.HasFlag(AnchorStyles.Right))
{
for (int y = 0; y < bmp.Height; y++)
// Find Width (Right to left)
for (int x = bmp.Width - 1; x >= 0 && !leave; x--)
{
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
for (int y = 0; y < bmp.Height; y++)
{
crop.Width = x - crop.X + 1;
leave = true;
break;
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.Width = x - crop.X + 1;
leave = true;
break;
}
}
}
leave = false;
}
leave = false;
// Find Height (Bottom to top)
for (int y = bmp.Height - 1; y >= 0 && !leave; y--)
if (sides.HasFlag(AnchorStyles.Bottom))
{
for (int x = 0; x < bmp.Width; x++)
// Find Height (Bottom to top)
for (int y = bmp.Height - 1; y >= 0 && !leave; y--)
{
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
for (int x = 0; x < bmp.Width; x++)
{
crop.Height = y - crop.Y + 1;
leave = true;
break;
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.Height = y - crop.Y + 1;
leave = true;
break;
}
}
}
}
@ -1803,10 +1824,11 @@ 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);
}
}
}