{"record":{"id":"9f49f0c43f7a8aef","repo":"dotnet/machinelearning","slug":"couldn-t-resize-the-image","errorCode":null,"errorMessage":"Couldn't resize the image","messagePattern":"Couldn't resize the image","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":306,"sourceCode":"            _image = image1;\n            return MLPixelFormat.Bgra32;\n        }\n\n        internal MLImage CloneWithResizing(int width, int height, ImageResizeMode mode)\n        {\n            ThrowInvalidOperationExceptionIfDisposed();\n\n            SKBitmap image = mode switch\n            {\n                ImageResizeMode.Pad => ResizeWithPadding(width, height),\n                ImageResizeMode.Fill => ResizeFull(width, height),\n                >= ImageResizeMode.CropAnchorTop and <= ImageResizeMode.CropAnchorCentral => ResizeWithCrop(width, height, mode),\n                _ => throw new ArgumentException($\"Invalid resize mode value.\", nameof(mode))\n            };\n\n            if (image is null)\n            {\n                throw new InvalidOperationException($\"Couldn't resize the image\");\n            }\n\n            return new MLImage(image);\n        }\n\n        private SKBitmap ResizeFull(int width, int height) => _image.Resize(new SKSizeI(width, height), new SKSamplingOptions(SKFilterMode.Nearest));\n\n        private SKBitmap ResizeWithPadding(int width, int height)\n        {\n            float widthAspect = (float)width / _image.Width;\n            float heightAspect = (float)height / _image.Height;\n            int destX = 0;\n            int destY = 0;\n            float aspect;\n\n            if (heightAspect < widthAspect)\n            {\n                aspect = heightAspect;","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L288-L324","documentation":"After dispatching to ResizeWithPadding/ResizeFull/ResizeWithCrop, CloneWithResizing checks the resulting SKBitmap. If the resize helper returns null (SkiaSharp's Resize failed), the method throws InvalidOperationException because it cannot produce the resized clone.","triggerScenarios":"CloneWithResizing where SKBitmap.Resize fails internally — e.g. zero/negative width or height, degenerate source bitmap, or Skia allocation failure for very large target dimensions.","commonSituations":"Passing 0 or negative target sizes from bad config; requesting huge dimensions exhausting memory; resizing an already-disposed or empty bitmap.","solutions":["Ensure width and height are positive and within reasonable memory limits","Verify the source image is valid and not disposed before resizing","Catch InvalidOperationException and fall back to a smaller target size or skip the resize"],"exampleFix":"// before\nvar resized = image.CloneWithResizing(0, 224, ImageResizeMode.Fill);\n// after\nif (width > 0 && height > 0)\n    var resized = image.CloneWithResizing(width, height, ImageResizeMode.Fill);","handlingStrategy":"validation","validationCode":"if (width <= 0 || height <= 0) throw new ArgumentOutOfRangeException(nameof(width), \"resize target must be positive\");","typeGuard":null,"tryCatchPattern":"try { var r = image.CloneWithResizing(w, h, mode); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Couldn't resize\")) { /* retry smaller or skip */ }","preventionTips":["Validate target dimensions are positive and memory-plausible","Ensure the source image is valid and not disposed","Cap resize dimensions to model requirements (e.g. 224x224)"],"tags":["csharp","image","resize"],"backgroundTag":"invalid-argument-value","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}