{"record":{"id":"2a08c54e9b0bbed0","repo":"dotnet/machinelearning","slug":"invalid-height-value","errorCode":null,"errorMessage":"Invalid height value.","messagePattern":"Invalid height value\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":95,"sourceCode":"        /// <param name=\"height\">The height of the image in pixels.</param>\n        /// <param name=\"pixelFormat\">The pixel format to create the image with.</param>\n        /// <param name=\"imagePixelData\">The pixels data to create the image from.</param>\n        /// <returns>MLImage object.</returns>\n        public static unsafe MLImage CreateFromPixels(int width, int height, MLPixelFormat pixelFormat, ReadOnlySpan<byte> imagePixelData)\n        {\n            if (pixelFormat != MLPixelFormat.Bgra32 && pixelFormat != MLPixelFormat.Rgba32)\n            {\n                throw new ArgumentException($\"Unsupported pixel format\", nameof(pixelFormat));\n            }\n\n            if (width <= 0)\n            {\n                throw new ArgumentException($\"Invalid width value.\", nameof(width));\n            }\n\n            if (height <= 0)\n            {\n                throw new ArgumentException($\"Invalid height value.\", nameof(height));\n            }\n\n            if (imagePixelData.Length != width * height * 4)\n            {\n                throw new ArgumentException($\"Invalid {nameof(imagePixelData)} buffer size.\");\n            }\n\n            SKBitmap image = new SKBitmap(new SKImageInfo(width, height, pixelFormat == MLPixelFormat.Bgra32 ? SKColorType.Bgra8888 : SKColorType.Rgba8888));\n\n            Debug.Assert(image.Info.BitsPerPixel == 32);\n            Debug.Assert(image.RowBytes * image.Height == width * height * 4);\n\n            imagePixelData.CopyTo(new Span<byte>(image.GetPixels().ToPointer(), image.Width * image.Height * 4));\n\n            return new MLImage(image);\n        }\n\n        /// <summary>","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L77-L113","documentation":"MLImage.CreateFromPixels validates that the height is strictly positive after validating width; height <= 0 throws an ArgumentException 'Invalid height value.' naming the height parameter, since bitmap dimensions must be positive.","triggerScenarios":"Calling MLImage.CreateFromPixels with height = 0 or negative — uninitialized value, off-by-one or division truncation, or a swapped width/height argument order.","commonSituations":"Swapping width and height when converting between row-major/column-major representations; metadata reporting 0 height for streams not yet probed.","solutions":["Ensure height >= 1 before the call","Check argument order — width and height may be swapped at the call site","Validate decoded/probed dimensions before constructing the image"],"exampleFix":"// before\nvar img = MLImage.CreateFromPixels(w, header.Height, MLPixelFormat.Bgra32, data); // Height may be 0\n// after\nif (header.Height <= 0)\n    throw new InvalidOperationException(\"Source image has invalid height.\");\nvar img = MLImage.CreateFromPixels(w, header.Height, MLPixelFormat.Bgra32, data);","handlingStrategy":"validation","validationCode":"if (height <= 0)\n    throw new ArgumentOutOfRangeException(nameof(height), height, \"Height must be positive.\");","typeGuard":"bool IsValidImageDimensions(int w, int h) => w > 0 && h > 0;","tryCatchPattern":"try\n{\n    img = MLImage.CreateFromPixels(width, height, pixelFormat, data);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"height\")\n{\n    logger.LogError(ex, \"Invalid image height {H}\", height);\n}","preventionTips":["Validate dimensions when parsed from headers/metadata","Check argument order at call sites (width, height)","Guard against metadata reporting 0 for un-probed images"],"tags":["csharp","images","argument-validation","dimensions"],"backgroundTag":"argument-out-of-range","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}