dotnet/machinelearning · error · ArgumentException
Invalid imagePixelData buffer size.
Error message
Invalid imagePixelData buffer size.
What it means
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.
Source
Thrown at src/Microsoft.ML.ImageAnalytics/MLImage.cs:100
{
if (pixelFormat != MLPixelFormat.Bgra32 && pixelFormat != MLPixelFormat.Rgba32)
{
throw new ArgumentException($"Unsupported pixel format", nameof(pixelFormat));
}
if (width <= 0)
{
throw new ArgumentException($"Invalid width value.", nameof(width));
}
if (height <= 0)
{
throw new ArgumentException($"Invalid height value.", nameof(height));
}
if (imagePixelData.Length != width * height * 4)
{
throw new ArgumentException($"Invalid {nameof(imagePixelData)} buffer size.");
}
SKBitmap image = new SKBitmap(new SKImageInfo(width, height, pixelFormat == MLPixelFormat.Bgra32 ? SKColorType.Bgra8888 : SKColorType.Rgba8888));
Debug.Assert(image.Info.BitsPerPixel == 32);
Debug.Assert(image.RowBytes * image.Height == width * height * 4);
imagePixelData.CopyTo(new Span<byte>(image.GetPixels().ToPointer(), image.Width * image.Height * 4));
return new MLImage(image);
}
/// <summary>
/// Gets the pixel format for this Image.
/// </summary>
public MLPixelFormat PixelFormat
{
getView on GitHub (pinned to 7b76e69cf9)
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
Example fix
// before
MLImage.CreateFromPixels(width, height, rgbBytes); // rgbBytes.Length = w*h*3
// after
var bgra = new byte[width * height * 4];
for (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; }
MLImage.CreateFromPixels(width, height, bgra); Defensive patterns
Strategy: validation
Validate before calling
if (pixels == null || pixels.Length != width * height * 4)
throw new ArgumentException($"pixel buffer must be width*height*4 = {width * height * 4} bytes"); Type guard
bool IsValidPixelBuffer(byte[] b, int w, int h) => b != null && b.Length == w * h * 4;
Try / catch
try { var img = MLImage.CreateFromPixels(w, h, buf); } catch (ArgumentException ex) when (ex.Message.Contains("buffer size")) { /* repack buffer to BGRA32 */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Either input_ids or inputs_embeds must be set
- Dimension must be divisible by 2
- Directory "{0}" does not exist.
- Unsupported pixel format
- Invalid width value.
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/247e8a01551650fa.
Report an issue: GitHub.