diff --git a/src/Greenshot.Base/Core/ImageHelper.cs b/src/Greenshot.Base/Core/ImageHelper.cs
index a4055f2c9..38c77d950 100644
--- a/src/Greenshot.Base/Core/ImageHelper.cs
+++ b/src/Greenshot.Base/Core/ImageHelper.cs
@@ -190,12 +190,14 @@ namespace Greenshot.Base.Core
///
/// Private helper method for the FindAutoCropRectangle
///
- ///
- ///
- ///
+ /// IFastBitmap
+ /// Point
+ /// int
+ /// Rectangle with optional area to scan in
/// Rectangle
- private static Rectangle FindAutoCropRectangle(IFastBitmap fastBitmap, Point colorPoint, int cropDifference)
+ private static Rectangle FindAutoCropRectangle(IFastBitmap fastBitmap, Point colorPoint, int cropDifference, Rectangle? area = null)
{
+ area ??= new Rectangle(0, 0, fastBitmap.Width, fastBitmap.Height);
Rectangle cropRectangle = Rectangle.Empty;
Color referenceColor = fastBitmap.GetColorAt(colorPoint.X, colorPoint.Y);
Point min = new Point(int.MaxValue, int.MaxValue);
@@ -203,9 +205,9 @@ namespace Greenshot.Base.Core
if (cropDifference > 0)
{
- for (int y = 0; y < fastBitmap.Height; y++)
+ for (int y = area.Value.Top; y < area.Value.Bottom; y++)
{
- for (int x = 0; x < fastBitmap.Width; x++)
+ for (int x = area.Value.Left; x < area.Value.Right; x++)
{
Color currentColor = fastBitmap.GetColorAt(x, y);
int diffR = Math.Abs(currentColor.R - referenceColor.R);
@@ -225,9 +227,9 @@ namespace Greenshot.Base.Core
}
else
{
- for (int y = 0; y < fastBitmap.Height; y++)
+ for (int y = area.Value.Top; y < area.Value.Bottom; y++)
{
- for (int x = 0; x < fastBitmap.Width; x++)
+ for (int x = area.Value.Left; x < area.Value.Right; x++)
{
Color currentColor = fastBitmap.GetColorAt(x, y);
if (!referenceColor.Equals(currentColor))
@@ -243,7 +245,7 @@ namespace Greenshot.Base.Core
}
}
- if (!(Point.Empty.Equals(min) && max.Equals(new Point(fastBitmap.Width - 1, fastBitmap.Height - 1))))
+ if (!(Point.Empty.Equals(min) && max.Equals(new Point(area.Value.Width - 1, area.Value.Height - 1))))
{
if (!(min.X == int.MaxValue || min.Y == int.MaxValue || max.X == int.MinValue || min.X == int.MinValue))
{
@@ -257,18 +259,20 @@ namespace Greenshot.Base.Core
///
/// Get a rectangle for the image which crops the image of all colors equal to that on 0,0
///
- ///
- ///
+ /// Image
+ /// int
+ /// Rectangle with optional area
/// Rectangle
- public static Rectangle FindAutoCropRectangle(Image image, int cropDifference)
+ public static Rectangle FindAutoCropRectangle(Image image, int cropDifference, Rectangle? area = null)
{
+ area ??= new Rectangle(0, 0, image.Width, image.Height);
Rectangle cropRectangle = Rectangle.Empty;
var checkPoints = new List
{
- new Point(0, 0),
- new Point(0, image.Height - 1),
- new Point(image.Width - 1, 0),
- new Point(image.Width - 1, image.Height - 1)
+ new Point(area.Value.Left, area.Value.Top),
+ new Point(area.Value.Left, area.Value.Bottom - 1),
+ new Point(area.Value.Right - 1, area.Value.Top),
+ new Point(area.Value.Right - 1, area.Value.Bottom - 1)
};
// Top Left
// Bottom Left
@@ -279,7 +283,7 @@ namespace Greenshot.Base.Core
// find biggest area
foreach (Point checkPoint in checkPoints)
{
- var currentRectangle = FindAutoCropRectangle(fastBitmap, checkPoint, cropDifference);
+ var currentRectangle = FindAutoCropRectangle(fastBitmap, checkPoint, cropDifference, area);
if (currentRectangle.Width * currentRectangle.Height > cropRectangle.Width * cropRectangle.Height)
{
cropRectangle = currentRectangle;
@@ -295,7 +299,7 @@ namespace Greenshot.Base.Core
///
/// Bitmap
/// IEffect
- ///
+ /// Matrix
/// Bitmap
public static Image ApplyEffect(Image sourceImage, IEffect effect, Matrix matrix)
{
diff --git a/src/Greenshot.Editor/Drawing/Surface.cs b/src/Greenshot.Editor/Drawing/Surface.cs
index e8561ab4b..be40c7d1c 100644
--- a/src/Greenshot.Editor/Drawing/Surface.cs
+++ b/src/Greenshot.Editor/Drawing/Surface.cs
@@ -973,13 +973,14 @@ namespace Greenshot.Editor.Drawing
///
/// Auto crop the image
///
+ /// Rectangle with optional area to find a crop region
/// true if cropped
- public bool AutoCrop()
+ public bool AutoCrop(Rectangle? cropArea = null)
{
Rectangle cropRectangle;
using (Image tmpImage = GetImageForExport())
{
- cropRectangle = ImageHelper.FindAutoCropRectangle(tmpImage, conf.AutoCropDifference);
+ cropRectangle = ImageHelper.FindAutoCropRectangle(tmpImage, conf.AutoCropDifference, cropArea);
}
if (!IsCropPossible(ref cropRectangle, CropContainer.CropModes.AutoCrop))
@@ -1062,7 +1063,7 @@ namespace Greenshot.Editor.Drawing
/// check if a crop is possible
///
/// Rectangle adapted to the dimensions of the image
- ///
+ /// CropModes
/// true if this is possible
public bool IsCropPossible(ref Rectangle cropRectangle, CropContainer.CropModes cropMode)
{
diff --git a/src/Greenshot.Editor/Forms/ImageEditorForm.cs b/src/Greenshot.Editor/Forms/ImageEditorForm.cs
index bf6f35f3d..c99d8bd43 100644
--- a/src/Greenshot.Editor/Forms/ImageEditorForm.cs
+++ b/src/Greenshot.Editor/Forms/ImageEditorForm.cs
@@ -25,6 +25,7 @@ using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
+using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Greenshot.Base;
@@ -1598,12 +1599,14 @@ namespace Greenshot.Editor.Forms
private void InitCropMode(CropContainer.CropModes mode)
{
+ var cropArea = _surface.Elements.FirstOrDefault(c => c is CropContainer)?.Bounds;
+
_surface.DrawingMode = DrawingModes.None;
_surface.RemoveCropContainer();
if (mode == CropContainer.CropModes.AutoCrop)
{
- if (!_surface.AutoCrop())
+ if (!_surface.AutoCrop(cropArea))
{
//not AutoCrop possible automatic switch to default crop mode
_surface.DrawingMode = DrawingModes.Crop;