{"record":{"id":"6caa1e2be3dcea23","repo":"SubtitleEdit/subtitleedit","slug":"could-not-resize-image-6caa1e","errorCode":null,"errorMessage":"Could not resize image","messagePattern":"Could not resize image","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/Ocr/GoogleLens/Lens.cs","lineNumber":57,"sourceCode":"        }\n\n        var bitmapToProcess = bitmap;\n        var finalMime = \"image/png\";\n\n        const int MAX_DIMENSION = 1200;\n\n        // Only process if absolutely necessary\n        if (originalWidth > MAX_DIMENSION || originalHeight > MAX_DIMENSION)\n        {\n            // Calculate new dimensions maintaining aspect ratio\n            float ratio = Math.Min((float)MAX_DIMENSION / originalWidth, (float)MAX_DIMENSION / originalHeight);\n            int newWidth = (int)(originalWidth * ratio);\n            int newHeight = (int)(originalHeight * ratio);\n\n            using var resized = bitmap.Resize(new SKImageInfo(newWidth, newHeight), new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear));\n            if (resized == null)\n            {\n                throw new Exception(\"Could not resize image\");\n            }\n\n            var imageToProcessBuffer = resized.ToPngArray();\n            return await ScanByData(imageToProcessBuffer, finalMime, new[] { originalWidth, originalHeight }, twoLetterLanguageCode);\n        }\n\n        var buffer = bitmapToProcess.ToPngArray();\n        return await ScanByData(buffer, finalMime, [originalWidth, originalHeight], twoLetterLanguageCode);\n    }\n}\n","sourceCodeStart":39,"sourceCodeEnd":68,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Ocr/GoogleLens/Lens.cs#L39-L68","documentation":"Thrown in the Google Lens OCR pipeline when an image larger than MAX_DIMENSION (1200px) must be downscaled and SkiaSharp's SKBitmap.Resize returns null. Resize yields null when Skia cannot produce the target bitmap: an unsupported source color type, degenerate/zero target dimensions, or a memory allocation failure.","triggerScenarios":"ScanByBitmap is called with a bitmap whose width or height exceeds 1200. The computed ratio yields newWidth/newHeight that are 0 (extreme aspect/underflow), the source SKColorType is one Skia's scaler rejects (vendor/unknown/alpha-only), or the process is low on memory when allocating the destination bitmap.","commonSituations":"Screenshots or photos decoded from HEIF/RAW with unusual color types, very large source images on memory-constrained machines, or a SkiaSharp version regression in the bitmap scaler path.","solutions":["Guard that newWidth > 0 && newHeight > 0 before calling Resize; if zero, fall back to scanning the original buffer.","Normalize the source bitmap to a scaler-friendly color type (e.g. SKColorType.Bgra8888) before resizing.","Catch a null resized result and fall back to ScanByData on the original bitmapToProcess buffer instead of throwing.","Cap incoming image dimensions earlier in the pipeline to reduce resize pressure."],"exampleFix":"// before\nusing var resized = bitmap.Resize(new SKImageInfo(newWidth, newHeight), sampling);\nif (resized == null) throw new Exception(\"Could not resize image\");\n\n// after\nif (newWidth <= 0 || newHeight <= 0)\n    return await ScanByData(bitmapToProcess.ToPngArray(), finalMime, new[] { originalWidth, originalHeight }, twoLetterLanguageCode);\nvar normalized = bitmap.ColorType == SKColorType.Bgra8888 ? bitmap : bitmap.Copy(new SKImageInfo(bitmap.Width, bitmap.Height, SKColorType.Bgra8888));\nusing var resized = normalized.Resize(new SKImageInfo(newWidth, newHeight), sampling);\nif (resized == null)\n    return await ScanByData(bitmapToProcess.ToPngArray(), finalMime, new[] { originalWidth, originalHeight }, twoLetterLanguageCode);","handlingStrategy":"validation","validationCode":"if (newWidth <= 0 || newHeight <= 0)\n    return await ScanByData(bitmapToProcess.ToPngArray(), finalMime, new[] { originalWidth, originalHeight }, twoLetterLanguageCode);\nif (bitmap.ColorType != SKColorType.Bgra8888)\n    bitmap = bitmap.Copy(new SKImageInfo(bitmap.Width, bitmap.Height, SKColorType.Bgra8888));","typeGuard":null,"tryCatchPattern":"try { return await lens.ScanByBitmap(bitmap, lang); }\ncatch (Exception ex) when (ex.Message == \"Could not resize image\")\n{\n    return await lens.ScanByData(bitmap.ToPngArray(), \"image/png\", new[] { bitmap.Width, bitmap.Height }, lang);\n}","preventionTips":["Validate target dimensions are > 0 before calling Resize.","Normalize source color type to a scaler-friendly value before resizing.","Cap incoming image dimensions earlier in the pipeline.","Fall back to the original buffer instead of throwing when resize fails."],"tags":["skia","image-processing","ocr","google-lens","null-check"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}