dotnet/machinelearning · error · ArgumentException

Invalid resize mode value.

Error message

Invalid resize mode value.

What it means

CloneWithResizing switches on the ImageResizeMode value: Pad, Fill, or the contiguous Crop-anchor range (CropAnchorTop..CropAnchorCentral). Any other mode value falls through to the discard arm and throws ArgumentException.

Source

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

                throw new InvalidOperationException("Unsupported image format.");
            }

            SKBitmap image1 = _image.Copy(SKColorType.Bgra8888);
            _image.Dispose();
            _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;

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Pass a valid ImageResizeMode (Pad, Fill, or a supported crop anchor value)
  2. Validate the enum value against the supported range before calling
  3. Update code/config after any enum changes and re-check serialized values

Example fix

// before
var resized = image.CloneWithResizing(224, 224, (ImageResizeMode)7); // unknown
// after
ImageResizeMode mode = (ImageResizeMode)7;
if (mode < ImageResizeMode.Pad || mode > ImageResizeMode.CropAnchorCentral) mode = ImageResizeMode.Fill;
var resized = image.CloneWithResizing(224, 224, mode);
Defensive patterns

Strategy: validation

Validate before calling

if (mode is not ImageResizeMode.Pad and not ImageResizeMode.Fill
    and not (mode is >= ImageResizeMode.CropAnchorTop and <= ImageResizeMode.CropAnchorCentral))
    mode = ImageResizeMode.Fill;

Type guard

bool IsValidResizeMode(ImageResizeMode m) => m == ImageResizeMode.Pad || m == ImageResizeMode.Fill || (m >= ImageResizeMode.CropAnchorTop && m <= ImageResizeMode.CropAnchorCentral);

Try / catch

try { var r = image.CloneWithResizing(w, h, mode); } catch (ArgumentException ex) when (ex.Message.Contains("resize mode")) { var r = image.CloneWithResizing(w, h, ImageResizeMode.Fill); }

Prevention

When it happens

Trigger: Calling CloneWithResizing with an ImageResizeMode that is not one of the defined handling modes — e.g. an out-of-range crop anchor, an undefined enum member cast from an int, or an unassigned default.

Common situations: Persisting resize mode as an int in config and deserializing a stale/unknown value; adding a new enum member without updating this switch; arithmetic that yields an intermediate anchor value outside the supported range.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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