SubtitleEdit/subtitleedit · error · Exception

Could not determine image dimensions

Error message

Could not determine image dimensions

What it means

Thrown by Core.ScanByBufferAsync when Helper.ImageDimensionsFromData returns (0, 0) for the buffer. This indicates SKCodec could not determine width/height — typically the codec created successfully but reported a degenerate info, or the buffer passed the earlier format probe yet fails dimension extraction. Lens needs real dimensions to build its request, so zero-size is rejected.

Source

Thrown at src/ui/Logic/Ocr/GoogleLens/Core.cs:81

        
        try
        {
            using var stream = new MemoryStream(buffer);
            using var codec = SKCodec.Create(stream);
            if (codec != null)
            {
                format = codec.EncodedFormat;
            }
        }
        catch
        {
            throw new Exception("File type not supported");
        }

        var (Width, Height) = Helper.ImageDimensionsFromData(buffer);
        if (Width == 0 && Height == 0)
        {
            throw new Exception("Could not determine image dimensions");
        }

        // Google Lens does not accept images larger than 1000x1000
        if (Width > 1000 || Height > 1000)
        {
            buffer = await Helper.ResizeImageAsync(buffer, 1000, 1000);
        }

        string mimeType = format switch
        {
            SKEncodedImageFormat.Png => "image/png",
            SKEncodedImageFormat.Jpeg => "image/jpeg",
            SKEncodedImageFormat.Webp => "image/webp",
            _ => "image/png"
        };

        return await ScanByData(buffer, mimeType, [Width, Height]);
    }

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Pre-validate the image with a full SKBitmap.Decode and check Width/Height > 0 before submitting to Lens.
  2. Re-export the image from a known-good source.
  3. Filter out zero-dimension images in the UI before OCR.
  4. Log the buffer length and first bytes to identify the malformed input.

Example fix

// before
var result = await core.ScanByBufferAsync(fileBytes);

// after
using var bmp = SKBitmap.Decode(new MemoryStream(fileBytes));
if (bmp == null || bmp.Width == 0 || bmp.Height == 0) throw new InvalidDataException("Invalid image");
var result = await core.ScanByBufferAsync(fileBytes);
Defensive patterns

Strategy: validation

Validate before calling

using var bmp = SKBitmap.Decode(new MemoryStream(buffer));
if (bmp == null || bmp.Width == 0 || bmp.Height == 0)
    throw new InvalidDataException("Image has no determinable dimensions");

Type guard

static bool HasValidDimensions(byte[] b) { using var c = SKCodec.Create(new MemoryStream(b)); return c != null && c.Info.Width > 0 && c.Info.Height > 0; }

Try / catch

try { var r = await core.ScanByBufferAsync(buffer); }
catch (Exception ex) when (ex.Message == "Could not determine image dimensions") { /* ask for a valid image */ }

Prevention

When it happens

Trigger: An image that SkiaSharp partially recognizes but cannot size (corrupt header, zero-dimension metadata, or a container with no frame data), or a buffer where the codec path diverges between SKCodec.Create and ImageDimensionsFromData.

Common situations: A truncated image whose header decodes but pixel data is missing; an image with explicit 0x0 in metadata; an edge-case format SkiaSharp misreports.

Related errors


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