Table of Contents

Method CropAuto

Namespace
LMKit.Media.Image
Assembly
LM-Kit.NET.dll

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

margin int

Optional extra margin (in pixels) to retain inside the crop on all four sides. Use 0 to keep only the content without extra padding.

tolerance byte

Optional per-channel absolute tolerance used when comparing pixels with the border color. Useful for scanner noise or compression artifacts; 0 means exact match.

Returns

ImageBuffer

A new ImageBuffer containing the cropped image, or null if:

  • 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 ARGB32, 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 ARGB32.