{"record":{"id":"e8ed73f378840528","repo":"dotnet/machinelearning","slug":"invalid-width-value","errorCode":null,"errorMessage":"Invalid width value.","messagePattern":"Invalid width value\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":90,"sourceCode":"\n        /// <summary>\n        /// Creates MLImage object from the pixel data span.\n        /// </summary>\n        /// <param name=\"width\">The width of the image in pixels.</param>\n        /// <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));","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L72-L108","documentation":"MLImage.CreateFromPixels validates that the width is strictly positive after checking the pixel format; width <= 0 throws an ArgumentException 'Invalid width value.' naming the width parameter, because a bitmap cannot have a non-positive dimension.","triggerScenarios":"Calling MLImage.CreateFromPixels with width = 0 or negative — e.g. an uninitialized dimension variable, integer division truncation yielding 0, or swapped/negative parsed values from config.","commonSituations":"Parsing width from a header/JSON where 0 means 'unknown'; computing width from a failed decode or empty metadata; sign errors when converting from unsigned sizes.","solutions":["Ensure width >= 1 (and height >= 1) before the call","Validate parsed dimensions at the data boundary and reject/skip frames with non-positive size","Check the source of the dimension (header parse, division) for logic errors"],"exampleFix":"// before\nint w = header.Width / scale; // can be 0\nvar img = MLImage.CreateFromPixels(w, h, MLPixelFormat.Bgra32, data);\n// after\nif (w <= 0 || h <= 0)\n    throw new InvalidOperationException($\"Cannot create image of size {w}x{h}.\");\nvar img = MLImage.CreateFromPixels(w, h, MLPixelFormat.Bgra32, data);","handlingStrategy":"validation","validationCode":"if (width <= 0)\n    throw new ArgumentOutOfRangeException(nameof(width), width, \"Width 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 == \"width\")\n{\n    logger.LogError(ex, \"Invalid image width {W}\", width);\n}","preventionTips":["Validate dimensions when parsed from headers/metadata","Watch for division truncation producing 0","Check for swapped width/height arguments"],"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"}