public static Bitmap CropBackground([NotNull] Bitmap value, Color backColor, DimensionRestriction restriction = DimensionRestriction.None, int unitWidthLimit = 0, int unitHeightLimit = 0) { (Point TopLeft, Point BottomRight)bounds = FindImageBounds(value, backColor, restriction, unitWidthLimit, unitHeightLimit); if (bounds.TopLeft.IsEmpty && bounds.BottomRight.IsEmpty) { return(null); } if (bounds.TopLeft.IsEmpty && bounds.BottomRight.X == value.Width && bounds.BottomRight.Y == value.Height) { return(Clone(value)); } int diffX = bounds.BottomRight.X - bounds.TopLeft.X + 1; int diffY = bounds.BottomRight.Y - bounds.TopLeft.Y + 1; Rectangle destRect = new Rectangle(0, 0, diffX, diffY); return(Crop(value, destRect)); }
public static Image CropBackground([NotNull] Image value, Color backColor, DimensionRestriction restriction = DimensionRestriction.None, int unitWidthLimit = 0, int unitHeightLimit = 0) { Bitmap bmp = value as Bitmap; bool created = bmp == null; try { if (created) { bmp = new Bitmap(value); } return(BitmapHelper.CropBackground(bmp, backColor, restriction, unitWidthLimit, unitHeightLimit)); } finally { if (created) { ObjectHelper.Dispose(ref bmp); } } }
public static Image Crop([NotNull] Image value, Rectangle rectangle, DimensionRestriction restriction = DimensionRestriction.None) { Bitmap bmp = value as Bitmap; bool created = bmp == null; try { if (created) { bmp = new Bitmap(value); } return(BitmapHelper.Crop(bmp, rectangle, restriction)); } finally { if (created) { ObjectHelper.Dispose(ref bmp); } } }
public static (Point TopLeft, Point BottomRight) FindImageBounds([NotNull] Bitmap value, Color backColor, DimensionRestriction restriction = DimensionRestriction.None, int unitWidthLimit = 0, int unitHeightLimit = 0) { if (value.Width < 2 || value.Height < 2) { return(Point.Empty, Point.Empty); } int width = value.Width, height = value.Height; Point topLeft = new Point(); Point bottomRight = new Point(width, height); unitWidthLimit = unitWidthLimit.Within(0, width); unitHeightLimit = unitHeightLimit.Within(0, height); if (unitWidthLimit == width) { restriction |= DimensionRestriction.KeepWidth; } if (unitHeightLimit == height) { restriction |= DimensionRestriction.KeepHeight; } bool kw = restriction.FastHasFlag(DimensionRestriction.KeepWidth); bool kh = restriction.FastHasFlag(DimensionRestriction.KeepHeight); if (kw && kh) { return(topLeft, bottomRight); } bool kuw = unitWidthLimit > 0; bool kuh = unitHeightLimit > 0; bool found = false; if (!kuw || !kuh) { for (int y = 0; y < height && !found; y++) { for (int x = 0; x < width && !found; x++) { Color c = value.GetPixel(x + 1, y + 1); if (c.IsSame(backColor)) { continue; } if (!kw && !kuw) { topLeft.X = x; } if (!kh && !kuh) { topLeft.Y = y; } found = true; } } found = false; } for (int y = height; y > 0 && !found; y--) { for (int x = width; x > 0 && !found; x--) { Color c = value.GetPixel(x - 1, y - 1); if (c.IsSame(backColor)) { continue; } if (!kw) { bottomRight.X = x; } if (!kh) { bottomRight.Y = y; } found = true; } } if (kuw && !kw) { bottomRight.X = bottomRight.X.Multiplier(unitWidthLimit); } if (kuh && !kh) { bottomRight.Y = bottomRight.Y.Multiplier(unitHeightLimit); } return(topLeft, bottomRight); }
public static Bitmap CropBackground([NotNull] Bitmap value, DimensionRestriction restriction = DimensionRestriction.None, int unitWidthLimit = 0, int unitHeightLimit = 0) { Color?backColor = GetMatchedBackColor(value); return(!backColor.HasValue ? value : CropBackground(value, backColor.Value, restriction, unitWidthLimit, unitHeightLimit)); }
public static Bitmap Crop([NotNull] Bitmap value, Rectangle rectangle, DimensionRestriction restriction = DimensionRestriction.None) { // Check source format if (!__formatTranslations.TryGetValue(value.PixelFormat, out PixelFormat formatTranslation)) { throw new UnsupportedImageFormatException(); } if (value.Width == 0 || value.Height == 0 || rectangle.IsEmpty) { return(null); } int width = value.Width, height = value.Height; if (restriction.FastHasFlag(DimensionRestriction.KeepWidth)) { rectangle.X = 0; rectangle.Width = width; } if (restriction.FastHasFlag(DimensionRestriction.KeepHeight)) { rectangle.Y = 0; rectangle.Height = height; } if (rectangle.IsEmpty || rectangle.Width <= 0 || rectangle.Height <= 0) { return(null); } if (rectangle.X == 0 && rectangle.Y == 0 && rectangle.Width == width && rectangle.Height == height) { return(Clone(value)); } if (rectangle.X + rectangle.Width > width) { rectangle.Width = width - rectangle.X; } if (rectangle.Y + rectangle.Height > height) { rectangle.Height = height - rectangle.Y; } BitmapData imageData = value.LockBits(new Rectangle(0, 0, value.Width, value.Height), ImageLockMode.ReadOnly, value.PixelFormat); Bitmap bitmap; try { Size newImageSize = rectangle.Size; bitmap = formatTranslation == PixelFormat.Format8bppIndexed ? CreateGreyscaleImage(newImageSize.Width, newImageSize.Height) : new Bitmap(newImageSize.Width, newImageSize.Height, formatTranslation); BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, newImageSize.Width, newImageSize.Height), ImageLockMode.ReadWrite, formatTranslation); try { // Process filter Crop(new UnmanagedImage(imageData), new UnmanagedImage(bitmapData), rectangle); } finally { bitmap.UnlockBits(bitmapData); } if (value.HorizontalResolution > 0.0) { if (value.VerticalResolution > 0.0) { bitmap.SetResolution(value.HorizontalResolution, value.VerticalResolution); } } } finally { value.UnlockBits(imageData); } return(bitmap); }