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 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

margin int

Pixels of border kept around the detected content.

tolerance byte

Per-channel distance a border pixel may sit from the corner color.

cropX int

Left edge of the crop region in the source image; 0 when nothing was cropped.

cropY int

Top 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.

Share