dotnet/machinelearning · error · InvalidOperationException

Couldn't resize the image

Error message

Couldn't resize the image

What it means

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.

Solutions

  1. Ensure width and height are positive and within reasonable memory limits
  2. Verify the source image is valid and not disposed before resizing
  3. Catch InvalidOperationException and fall back to a smaller target size or skip the resize

Example fix

// before
var resized = image.CloneWithResizing(0, 224, ImageResizeMode.Fill);
// after
if (width > 0 && height > 0)
    var resized = image.CloneWithResizing(width, height, ImageResizeMode.Fill);
Defensive patterns

Strategy: validation

Validate before calling

if (width <= 0 || height <= 0) throw new ArgumentOutOfRangeException(nameof(width), "resize target must be positive");

Try / catch

try { var r = image.CloneWithResizing(w, h, mode); } catch (InvalidOperationException ex) when (ex.Message.Contains("Couldn't resize")) { /* retry smaller or skip */ }

Prevention

When it happens

Trigger: 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.

Common situations: Passing 0 or negative target sizes from bad config; requesting huge dimensions exhausting memory; resizing an already-disposed or empty bitmap.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/9f49f0c43f7a8aef. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.ML.ImageAnalytics/MLImage.cs:306

            _image = image1;
            return MLPixelFormat.Bgra32;
        }

        internal MLImage CloneWithResizing(int width, int height, ImageResizeMode mode)
        {
            ThrowInvalidOperationExceptionIfDisposed();

            SKBitmap image = mode switch
            {
                ImageResizeMode.Pad => ResizeWithPadding(width, height),
                ImageResizeMode.Fill => ResizeFull(width, height),
                >= ImageResizeMode.CropAnchorTop and <= ImageResizeMode.CropAnchorCentral => ResizeWithCrop(width, height, mode),
                _ => throw new ArgumentException($"Invalid resize mode value.", nameof(mode))
            };

            if (image is null)
            {
                throw new InvalidOperationException($"Couldn't resize the image");
            }

            return new MLImage(image);
        }

        private SKBitmap ResizeFull(int width, int height) => _image.Resize(new SKSizeI(width, height), new SKSamplingOptions(SKFilterMode.Nearest));

        private SKBitmap ResizeWithPadding(int width, int height)
        {
            float widthAspect = (float)width / _image.Width;
            float heightAspect = (float)height / _image.Height;
            int destX = 0;
            int destY = 0;
            float aspect;

            if (heightAspect < widthAspect)
            {
                aspect = heightAspect;

View on GitHub (pinned to 7b76e69cf9)