SubtitleEdit/subtitleedit · error · ArgumentException

Image dimensions [width, height] not set or invalid

Error message

Image dimensions [width, height] not set or invalid

What it means

Thrown by the BoundingBox constructor when imageDimensions is null or does not have exactly 2 elements (width, height). These pixel dimensions are required by ToPixelCoords to convert normalized Lens coordinates into absolute pixels; without a valid pair the conversion cannot run, so the constructor refuses to build a half-initialized object.

Source

Thrown at src/ui/Logic/Ocr/GoogleLens/BoundingBox.cs:24

{
    private readonly int[] _imageDimensions;

    public double CenterPerX { get; }
    public double CenterPerY { get; }
    public double PerWidth { get; }
    public double PerHeight { get; }
    public PixelCoordinates PixelCoords { get; }

    public BoundingBox(double[] box, int[] imageDimensions)
    {
        if (box == null || box.Length != 4)
        {
            throw new ArgumentException("Bounding box array [centerPerX, centerPerY, perWidth, perHeight] not set or invalid");
        }
        
        if (imageDimensions == null || imageDimensions.Length != 2)
        {
            throw new ArgumentException("Image dimensions [width, height] not set or invalid");
        }

        _imageDimensions = imageDimensions;
        CenterPerX = box[0];
        CenterPerY = box[1];
        PerWidth = box[2];
        PerHeight = box[3];
        PixelCoords = ToPixelCoords();
    }

    private PixelCoordinates ToPixelCoords()
    {
        var imgWidth = _imageDimensions[0];
        var imgHeight = _imageDimensions[1];

        var width = PerWidth * imgWidth;
        var height = PerHeight * imgHeight;

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Always pass a 2-element int[] [width, height] derived from the source bitmap or image header.
  2. Null-check imageDimensions at the call site before constructing.
  3. Validate Length == 2 in the Lens parsing layer before creating boxes.
  4. Centralize dimension extraction (Helper.ImageDimensionsFromData) and reuse the result.

Example fix

// before
var box = new BoundingBox(region, dimsArray);

// after
if (dimsArray == null || dimsArray.Length != 2) throw new ArgumentException("dims");
var box = new BoundingBox(region, dimsArray);
Defensive patterns

Strategy: validation

Validate before calling

if (imageDimensions == null || imageDimensions.Length != 2)
    throw new ArgumentException("imageDimensions must be [width, height]");
var bb = new BoundingBox(box, imageDimensions);

Type guard

static bool IsValidDims(int[] dims) => dims != null && dims.Length == 2 && dims[0] > 0 && dims[1] > 0;

Try / catch

try { var bb = new BoundingBox(box, dims); }
catch (ArgumentException ex) when (ex.Message.Contains("Image dimensions")) { /* skip or recompute dims */ }

Prevention

When it happens

Trigger: Calling new BoundingBox(box, imageDimensions) with imageDimensions null or of a length other than 2 — e.g. passing the original bitmap's single dimension, a 3-element array, or a null from an uninitialized caller.

Common situations: The caller forgot to populate image dimensions; the upstream ScanByData/ScanByBitmap did not thread the original dimensions through; a refactor changed the dimension carrier type.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/402dd71cfd60b425. Report an issue: GitHub.