{"record":{"id":"247e13412d71e9e6","repo":"SubtitleEdit/subtitleedit","slug":"could-not-resize-image","errorCode":null,"errorMessage":"Could not resize image","messagePattern":"Could not resize image","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/Ocr/GoogleLens/Helper.cs","lineNumber":32,"sourceCode":"        using var original = SKBitmap.Decode(inputStream);\n        \n        if (original == null)\n        {\n            throw new InvalidOperationException(\"Could not decode image\");\n        }\n\n        // Calculate new dimensions maintaining aspect ratio\n        float ratioX = (float)maxWidth / original.Width;\n        float ratioY = (float)maxHeight / original.Height;\n        float ratio = Math.Min(ratioX, ratioY);\n        \n        int newWidth = (int)(original.Width * ratio);\n        int newHeight = (int)(original.Height * ratio);\n\n        using var resized = original.Resize(new SKImageInfo(newWidth, newHeight), new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear));\n        if (resized == null)\n        {\n            throw new InvalidOperationException(\"Could not resize image\");\n        }\n            \n        using var image = SKImage.FromBitmap(resized);\n        using var data = image.Encode(SKEncodedImageFormat.Jpeg, 90);\n        \n        return await Task.FromResult(data.ToArray());\n    }\n    \n    public static (int Width, int Height) ImageDimensionsFromData(byte[] data)\n    {\n        using var inputStream = new MemoryStream(data);\n        using var codec = SKCodec.Create(inputStream);\n        \n        if (codec == null)\n        {\n            throw new InvalidOperationException(\"Could not decode image\");\n        }\n            ","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Ocr/GoogleLens/Helper.cs#L14-L50","documentation":"Thrown by Helper.ResizeImageAsync when SKBitmap.Resize returns null. The source decoded fine, but the resize operation (using SKSamplingOptions with Linear filter + Linear mipmap) failed to produce a destination bitmap — typically due to a degenerate target size (newWidth/newHeight computed as 0), an unsupported source color type, or a native resize failure.","triggerScenarios":"Resizing an image whose computed newWidth or newHeight rounds to 0 (very small source or extreme ratio), or a source bitmap whose color type SkiaSharp cannot resample. The integer cast (int)(width*ratio) can yield 0 for tiny inputs.","commonSituations":"A 1x1 or sub-1px source image; maxWidth/maxHeight passed as 0; a grayscale/alpha-only bitmap the resizer rejects; a SkiaSharp version-specific resize bug.","solutions":["Clamp newWidth/newHeight to a minimum of 1 before resizing.","Validate maxWidth/maxHeight > 0 and source dimensions > 0 up front.","Skip resize if the source already fits within the target bounds.","Fall back to SKImage resize or returning the original if bitmap resize fails."],"exampleFix":"// before\nint newWidth = (int)(original.Width * ratio);\nint newHeight = (int)(original.Height * ratio);\n\n// after\nint newWidth = Math.Max(1, (int)(original.Width * ratio));\nint newHeight = Math.Max(1, (int)(original.Height * ratio));","handlingStrategy":"validation","validationCode":"int newWidth = Math.Max(1, (int)(original.Width * ratio));\nint newHeight = Math.Max(1, (int)(original.Height * ratio));\nif (newWidth < 1 || newHeight < 1) throw new ArgumentException(\"Target size too small\");","typeGuard":"static bool IsValidResizeTarget(int w, int h) => w >= 1 && h >= 1;","tryCatchPattern":"try { var resized = await Helper.ResizeImageAsync(buffer, 1000, 1000); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Could not resize image\") { /* fall back to original or different filter */ }","preventionTips":["Clamp computed dimensions to a minimum of 1.","Validate maxWidth/maxHeight and source dimensions > 0.","Skip resize when the source already fits.","Fall back to SKImage-based resize on failure."],"tags":["google-lens","ocr","image","skiasharp","resize","edge-case"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}