mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
fixed #2757: Added auto crop image support to image editor
This commit is contained in:
parent
fd0bc4bfff
commit
3215de1fda
3 changed files with 132 additions and 2 deletions
|
@ -267,7 +267,7 @@ public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Automatically crop image to remove transparent outside area.</summary>
|
/// <summary>Automatically crop image to remove transparent outside area.</summary>
|
||||||
public static Bitmap AutoCropImage(Bitmap bmp)
|
public static Bitmap AutoCropTransparent(Bitmap bmp)
|
||||||
{
|
{
|
||||||
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||||
Rectangle rect = source;
|
Rectangle rect = source;
|
||||||
|
@ -354,7 +354,7 @@ public static Bitmap AutoCropImage(Bitmap bmp)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Automatically crop image to remove transparent outside area. Only checks center pixels.</summary>
|
/// <summary>Automatically crop image to remove transparent outside area. Only checks center pixels.</summary>
|
||||||
public static Bitmap QuickAutoCropImage(Bitmap bmp)
|
public static Bitmap QuickAutoCropTransparent(Bitmap bmp)
|
||||||
{
|
{
|
||||||
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||||
Rectangle rect = source;
|
Rectangle rect = source;
|
||||||
|
@ -1645,5 +1645,120 @@ public static void DrawColorPickerIcon(Graphics g, Color color, Rectangle rect,
|
||||||
g.DrawRectangleProper(Pens.Black, holeRect);
|
g.DrawRectangleProper(Pens.Black, holeRect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = false)
|
||||||
|
{
|
||||||
|
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||||
|
Rectangle crop = source;
|
||||||
|
|
||||||
|
using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
|
||||||
|
{
|
||||||
|
bool leave = false;
|
||||||
|
|
||||||
|
ColorBgra checkColor = unsafeBitmap.GetPixel(crop.X, crop.Y);
|
||||||
|
|
||||||
|
// Find X (Left to right)
|
||||||
|
for (int x = crop.X; x < crop.Width && !leave; x++)
|
||||||
|
{
|
||||||
|
for (int y = crop.Y; y < crop.Height; y++)
|
||||||
|
{
|
||||||
|
if (unsafeBitmap.GetPixel(x, y) != checkColor)
|
||||||
|
{
|
||||||
|
crop.X = x;
|
||||||
|
leave = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
leave = false;
|
||||||
|
|
||||||
|
if (!sameColorCrop)
|
||||||
|
{
|
||||||
|
checkColor = unsafeBitmap.GetPixel(crop.X, crop.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find Y (Top to bottom)
|
||||||
|
for (int y = crop.Y; y < crop.Height && !leave; y++)
|
||||||
|
{
|
||||||
|
for (int x = crop.X; x < crop.Width; x++)
|
||||||
|
{
|
||||||
|
if (unsafeBitmap.GetPixel(x, y) != checkColor)
|
||||||
|
{
|
||||||
|
crop.Y = y;
|
||||||
|
leave = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
leave = false;
|
||||||
|
|
||||||
|
if (!sameColorCrop)
|
||||||
|
{
|
||||||
|
checkColor = unsafeBitmap.GetPixel(crop.Width - 1, crop.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find Width (Right to left)
|
||||||
|
for (int x = crop.Width - 1; x >= crop.X && !leave; x--)
|
||||||
|
{
|
||||||
|
for (int y = crop.Y; y < crop.Height; y++)
|
||||||
|
{
|
||||||
|
if (unsafeBitmap.GetPixel(x, y) != checkColor)
|
||||||
|
{
|
||||||
|
crop.Width = x - crop.X + 1;
|
||||||
|
leave = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
leave = false;
|
||||||
|
|
||||||
|
if (!sameColorCrop)
|
||||||
|
{
|
||||||
|
checkColor = unsafeBitmap.GetPixel(crop.X, crop.Height - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find Height (Bottom to top)
|
||||||
|
for (int y = crop.Height - 1; y >= crop.Y && !leave; y--)
|
||||||
|
{
|
||||||
|
for (int x = crop.X; x < crop.Width; x++)
|
||||||
|
{
|
||||||
|
if (unsafeBitmap.GetPixel(x, y) != checkColor)
|
||||||
|
{
|
||||||
|
crop.Height = y - crop.Y + 1;
|
||||||
|
leave = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return crop;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If auto crop rectangle and source image rectangle is same then null will be returned.
|
||||||
|
/// After auto crop, source image will be disposed.
|
||||||
|
/// </summary>
|
||||||
|
public static Bitmap AutoCropImage(Bitmap bmp, bool sameColorCrop = false)
|
||||||
|
{
|
||||||
|
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||||
|
Rectangle rect = FindAutoCropRectangle(bmp, sameColorCrop);
|
||||||
|
|
||||||
|
if (source != rect)
|
||||||
|
{
|
||||||
|
Bitmap croppedBitmap = CropBitmap(bmp, rect);
|
||||||
|
|
||||||
|
if (croppedBitmap != null)
|
||||||
|
{
|
||||||
|
bmp.Dispose();
|
||||||
|
return croppedBitmap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1430,6 +1430,16 @@ private void ChangeCanvasSize()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AutoCropImage()
|
||||||
|
{
|
||||||
|
Bitmap bmp = ImageHelpers.AutoCropImage((Bitmap)Form.Canvas);
|
||||||
|
|
||||||
|
if (bmp != null)
|
||||||
|
{
|
||||||
|
UpdateCanvas(bmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void RotateImage(RotateFlipType type)
|
private void RotateImage(RotateFlipType type)
|
||||||
{
|
{
|
||||||
Image img = (Image)Form.Canvas.Clone();
|
Image img = (Image)Form.Canvas.Clone();
|
||||||
|
|
|
@ -618,6 +618,11 @@ internal void CreateToolbar()
|
||||||
tsmiCanvasSize.MouseDown += (sender, e) => ChangeCanvasSize();
|
tsmiCanvasSize.MouseDown += (sender, e) => ChangeCanvasSize();
|
||||||
tsddbImage.DropDownItems.Add(tsmiCanvasSize);
|
tsddbImage.DropDownItems.Add(tsmiCanvasSize);
|
||||||
|
|
||||||
|
ToolStripMenuItem tsmiAutoCropImage = new ToolStripMenuItem("Auto crop image");
|
||||||
|
tsmiAutoCropImage.Image = Resources.image_crop;
|
||||||
|
tsmiAutoCropImage.MouseDown += (sender, e) => AutoCropImage();
|
||||||
|
tsddbImage.DropDownItems.Add(tsmiAutoCropImage);
|
||||||
|
|
||||||
tsddbImage.DropDownItems.Add(new ToolStripSeparator());
|
tsddbImage.DropDownItems.Add(new ToolStripSeparator());
|
||||||
|
|
||||||
ToolStripMenuItem tsmiRotate90Clockwise = new ToolStripMenuItem(Resources.ShapeManager_CreateToolbar_Rotate90Clockwise);
|
ToolStripMenuItem tsmiRotate90Clockwise = new ToolStripMenuItem(Resources.ShapeManager_CreateToolbar_Rotate90Clockwise);
|
||||||
|
|
Loading…
Reference in a new issue