mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
Added sides option to auto crop image effect
This commit is contained in:
parent
6c696fd034
commit
599cfc2a0e
2 changed files with 77 additions and 46 deletions
|
@ -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);
|
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||||
|
|
||||||
|
if (sides == AnchorStyles.None)
|
||||||
|
{
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
Rectangle crop = source;
|
Rectangle crop = source;
|
||||||
|
|
||||||
using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
|
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 mask = checkColor.Alpha == 0 ? 0xFF000000 : 0xFFFFFFFF;
|
||||||
uint check = checkColor.Bgra & mask;
|
uint check = checkColor.Bgra & mask;
|
||||||
|
|
||||||
|
if (sides.HasFlag(AnchorStyles.Left))
|
||||||
|
{
|
||||||
// Find X (Left to right)
|
// Find X (Left to right)
|
||||||
for (int x = 0; x < bmp.Width && !leave; x++)
|
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)
|
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
|
||||||
{
|
{
|
||||||
crop.X = x;
|
crop.X = x;
|
||||||
|
crop.Width -= x;
|
||||||
leave = true;
|
leave = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1745,7 +1755,10 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
|
||||||
}
|
}
|
||||||
|
|
||||||
leave = false;
|
leave = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sides.HasFlag(AnchorStyles.Top))
|
||||||
|
{
|
||||||
// Find Y (Top to bottom)
|
// Find Y (Top to bottom)
|
||||||
for (int y = 0; y < bmp.Height && !leave; y++)
|
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)
|
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
|
||||||
{
|
{
|
||||||
crop.Y = y;
|
crop.Y = y;
|
||||||
|
crop.Height -= y;
|
||||||
leave = true;
|
leave = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1761,6 +1775,7 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
|
||||||
}
|
}
|
||||||
|
|
||||||
leave = false;
|
leave = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!sameColorCrop)
|
if (!sameColorCrop)
|
||||||
{
|
{
|
||||||
|
@ -1769,6 +1784,8 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
|
||||||
check = checkColor.Bgra & mask;
|
check = checkColor.Bgra & mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sides.HasFlag(AnchorStyles.Right))
|
||||||
|
{
|
||||||
// Find Width (Right to left)
|
// Find Width (Right to left)
|
||||||
for (int x = bmp.Width - 1; x >= 0 && !leave; x--)
|
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;
|
leave = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sides.HasFlag(AnchorStyles.Bottom))
|
||||||
|
{
|
||||||
// Find Height (Bottom to top)
|
// Find Height (Bottom to top)
|
||||||
for (int y = bmp.Height - 1; y >= 0 && !leave; y--)
|
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;
|
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 source = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||||
Rectangle rect = FindAutoCropRectangle(bmp, sameColorCrop);
|
Rectangle rect = FindAutoCropRectangle(bmp, sameColorCrop, sides);
|
||||||
|
|
||||||
if (source != rect)
|
if (source != rect)
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,15 +26,24 @@ You should have received a copy of the GNU General Public License
|
||||||
using ShareX.HelpersLib;
|
using ShareX.HelpersLib;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace ShareX.ImageEffectsLib
|
namespace ShareX.ImageEffectsLib
|
||||||
{
|
{
|
||||||
[Description("Auto crop")]
|
[Description("Auto crop")]
|
||||||
internal class AutoCrop : ImageEffect
|
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)
|
public override Image Apply(Image img)
|
||||||
{
|
{
|
||||||
return ImageHelpers.AutoCropImage((Bitmap)img);
|
return ImageHelpers.AutoCropImage((Bitmap)img, false, Sides);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue