Method CropAuto
CropAuto(int, byte)
Automatically crops uniform borders from the image by scanning from each edge until a pixel differs from the border color, then returns the cropped buffer. The border color is sampled from the corners with a simple majority/consensus, improving robustness over single-pixel sampling.
public ImageBuffer CropAuto(int margin = 0, byte tolerance = 0)
Parameters
marginintOptional extra margin (in pixels) to retain inside the crop on all four sides. Use
0to keep only the content without extra padding.tolerancebyteOptional per-channel absolute tolerance used when comparing pixels with the border color. Useful for scanner noise or compression artifacts;
0means exact match.
Returns
- ImageBuffer
A new ImageBuffer containing the cropped image, or
nullif:- No change is needed (no uniform border detected),
- The entire image matches the border color (empty page), or
- The computed crop is invalid (zero/negative width or height).
Remarks
For RGBA32, fully transparent pixels (alpha == 0) are always treated as border. RGB comparisons ignore alpha.
Exceptions
- NotSupportedException
Thrown if the pixel format is not one of GRAY8, RGB24, or RGBA32.
CropAuto(int, byte, out int, out int)
Same as CropAuto(int, byte) but also returns the top-left offset of the crop region within the source image, so a caller can report or reuse the content bounds without cropping again.
public ImageBuffer CropAuto(int margin, byte tolerance, out int cropX, out int cropY)
Parameters
marginintPixels of border kept around the detected content.
tolerancebytePer-channel distance a border pixel may sit from the corner color.
cropXintLeft edge of the crop region in the source image; 0 when nothing was cropped.
cropYintTop edge of the crop region in the source image; 0 when nothing was cropped.
Returns
- ImageBuffer
The cropped image, or null when there is nothing to trim.