{"record":{"id":"247e8a01551650fa","repo":"dotnet/machinelearning","slug":"invalid-imagepixeldata-buffer-size","errorCode":null,"errorMessage":"Invalid imagePixelData buffer size.","messagePattern":"Invalid imagePixelData buffer size\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":100,"sourceCode":"        {\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>\n        /// Gets the pixel format for this Image.\n        /// </summary>\n        public MLPixelFormat PixelFormat\n        {\n            get","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L82-L118","documentation":"MLImage.CreateFromPixels validates that the imagePixelData buffer length exactly equals width*height*4 (4 bytes per pixel: BGRA32/RGBA32). If the supplied byte array does not match the declared dimensions, ArgumentException is thrown to prevent reading out-of-bounds or producing a corrupt SKBitmap.","triggerScenarios":"Calling MLImage.CreateFromPixels with a byte[] whose Length != width*height*4 — e.g. passing a buffer sized for 3 bytes per pixel (RGB), a stride-padded buffer, or dimensions that don't match the data.","commonSituations":"Converting from codecs that output RGB24 instead of RGBA32, forgetting that the buffer includes no row padding, or mixing up width/height order so the product is wrong.","solutions":["Compute the expected size as width*height*4 and verify your buffer length before calling","Convert 3-byte-per-pixel data to 4-byte BGRA/RGBA before calling","If your source has row stride padding, copy row-by-row into a tightly packed buffer"],"exampleFix":"// before\nMLImage.CreateFromPixels(width, height, rgbBytes); // rgbBytes.Length = w*h*3\n// after\nvar bgra = new byte[width * height * 4];\nfor (int i = 0, j = 0; i < rgbBytes.Length; i += 3, j += 4) { bgra[j] = rgbBytes[i]; bgra[j+1] = rgbBytes[i+1]; bgra[j+2] = rgbBytes[i+2]; bgra[j+3] = 255; }\nMLImage.CreateFromPixels(width, height, bgra);","handlingStrategy":"validation","validationCode":"if (pixels == null || pixels.Length != width * height * 4)\n    throw new ArgumentException($\"pixel buffer must be width*height*4 = {width * height * 4} bytes\");","typeGuard":"bool IsValidPixelBuffer(byte[] b, int w, int h) => b != null && b.Length == w * h * 4;","tryCatchPattern":"try { var img = MLImage.CreateFromPixels(w, h, buf); } catch (ArgumentException ex) when (ex.Message.Contains(\"buffer size\")) { /* repack buffer to BGRA32 */ }","preventionTips":["Always allocate buffers as width*height*4 for 32-bit BGRA/RGBA","Convert RGB24 sources to 4-byte-per-pixel before wrapping","Strip row stride padding from decoded frames"],"tags":["csharp","image","argument-validation"],"backgroundTag":"invalid-argument-value","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"}