{"record":{"id":"000c31da41c2f979","repo":"dotnet/machinelearning","slug":"invalid-resize-mode-value","errorCode":null,"errorMessage":"Invalid resize mode value.","messagePattern":"Invalid resize mode value\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":301,"sourceCode":"                throw new InvalidOperationException(\"Unsupported image format.\");\n            }\n\n            SKBitmap image1 = _image.Copy(SKColorType.Bgra8888);\n            _image.Dispose();\n            _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;","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L283-L319","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a valid ImageResizeMode (Pad, Fill, or a supported crop anchor value)","Validate the enum value against the supported range before calling","Update code/config after any enum changes and re-check serialized values"],"exampleFix":"// before\nvar resized = image.CloneWithResizing(224, 224, (ImageResizeMode)7); // unknown\n// after\nImageResizeMode mode = (ImageResizeMode)7;\nif (mode < ImageResizeMode.Pad || mode > ImageResizeMode.CropAnchorCentral) mode = ImageResizeMode.Fill;\nvar resized = image.CloneWithResizing(224, 224, mode);","handlingStrategy":"validation","validationCode":"if (mode is not ImageResizeMode.Pad and not ImageResizeMode.Fill\n    and not (mode is >= ImageResizeMode.CropAnchorTop and <= ImageResizeMode.CropAnchorCentral))\n    mode = ImageResizeMode.Fill;","typeGuard":"bool IsValidResizeMode(ImageResizeMode m) => m == ImageResizeMode.Pad || m == ImageResizeMode.Fill || (m >= ImageResizeMode.CropAnchorTop && m <= ImageResizeMode.CropAnchorCentral);","tryCatchPattern":"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); }","preventionTips":["Whitelist enum values read from config before use","Never cast raw ints to ImageResizeMode without range checks","Update switch handling when adding enum members"],"tags":["csharp","image","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}