{"record":{"id":"768cac00006123c0","repo":"SubtitleEdit/subtitleedit","slug":"could-not-determine-image-dimensions","errorCode":null,"errorMessage":"Could not determine image dimensions","messagePattern":"Could not determine image dimensions","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/Ocr/GoogleLens/Core.cs","lineNumber":81,"sourceCode":"        \n        try\n        {\n            using var stream = new MemoryStream(buffer);\n            using var codec = SKCodec.Create(stream);\n            if (codec != null)\n            {\n                format = codec.EncodedFormat;\n            }\n        }\n        catch\n        {\n            throw new Exception(\"File type not supported\");\n        }\n\n        var (Width, Height) = Helper.ImageDimensionsFromData(buffer);\n        if (Width == 0 && Height == 0)\n        {\n            throw new Exception(\"Could not determine image dimensions\");\n        }\n\n        // Google Lens does not accept images larger than 1000x1000\n        if (Width > 1000 || Height > 1000)\n        {\n            buffer = await Helper.ResizeImageAsync(buffer, 1000, 1000);\n        }\n\n        string mimeType = format switch\n        {\n            SKEncodedImageFormat.Png => \"image/png\",\n            SKEncodedImageFormat.Jpeg => \"image/jpeg\",\n            SKEncodedImageFormat.Webp => \"image/webp\",\n            _ => \"image/png\"\n        };\n\n        return await ScanByData(buffer, mimeType, [Width, Height]);\n    }","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Ocr/GoogleLens/Core.cs#L63-L99","documentation":"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.","triggerScenarios":"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.","commonSituations":"A truncated image whose header decodes but pixel data is missing; an image with explicit 0x0 in metadata; an edge-case format SkiaSharp misreports.","solutions":["Pre-validate the image with a full SKBitmap.Decode and check Width/Height > 0 before submitting to Lens.","Re-export the image from a known-good source.","Filter out zero-dimension images in the UI before OCR.","Log the buffer length and first bytes to identify the malformed input."],"exampleFix":"// before\nvar result = await core.ScanByBufferAsync(fileBytes);\n\n// after\nusing var bmp = SKBitmap.Decode(new MemoryStream(fileBytes));\nif (bmp == null || bmp.Width == 0 || bmp.Height == 0) throw new InvalidDataException(\"Invalid image\");\nvar result = await core.ScanByBufferAsync(fileBytes);","handlingStrategy":"validation","validationCode":"using var bmp = SKBitmap.Decode(new MemoryStream(buffer));\nif (bmp == null || bmp.Width == 0 || bmp.Height == 0)\n    throw new InvalidDataException(\"Image has no determinable dimensions\");","typeGuard":"static bool HasValidDimensions(byte[] b) { using var c = SKCodec.Create(new MemoryStream(b)); return c != null && c.Info.Width > 0 && c.Info.Height > 0; }","tryCatchPattern":"try { var r = await core.ScanByBufferAsync(buffer); }\ncatch (Exception ex) when (ex.Message == \"Could not determine image dimensions\") { /* ask for a valid image */ }","preventionTips":["Fully decode the image once upstream to confirm dimensions.","Reject zero-dimension images in the UI.","Re-export corrupt images from a known-good source.","Log buffer length and first bytes for malformed inputs."],"tags":["google-lens","ocr","image","skiasharp","validation"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}