dotnet/machinelearning · error · ArgumentException

Unsupported pixel format

Error message

Unsupported pixel format

What it means

MLImage.CreateFromPixels only accepts Bgra32 or Rgba32 4-bytes-per-pixel formats, since it wraps the raw buffer directly. Any other MLPixelFormat is rejected with an ArgumentException naming pixelFormat before any other validation.

Source

Thrown at src/Microsoft.ML.ImageAnalytics/MLImage.cs:85

                throw new ArgumentException($"Invalid path", nameof(imagePath));
            }

            return new MLImage(image);
        }

        /// <summary>
        /// Creates MLImage object from the pixel data span.
        /// </summary>
        /// <param name="width">The width of the image in pixels.</param>
        /// <param name="height">The height of the image in pixels.</param>
        /// <param name="pixelFormat">The pixel format to create the image with.</param>
        /// <param name="imagePixelData">The pixels data to create the image from.</param>
        /// <returns>MLImage object.</returns>
        public static unsafe MLImage CreateFromPixels(int width, int height, MLPixelFormat pixelFormat, ReadOnlySpan<byte> imagePixelData)
        {
            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));

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Convert the buffer to Bgra32 or Rgba32 (add an alpha byte, expanding 3bpp to 4bpp) before calling
  2. Confirm the source reports its own format instead of assuming; map it explicitly to Bgra32/Rgba32
  3. Use MLImage.CreateFromStream/CreateFromFile for non-32bpp sources so Skia converts internally

Example fix

// before
var img = MLImage.CreateFromPixels(w, h, MLPixelFormat.Rgb24, rgbBytes); // throws
// after
byte[] bgra = new byte[w * h * 4];
for (int i = 0; i < w * h; i++)
{
    bgra[i * 4 + 0] = rgbBytes[i * 3 + 2];
    bgra[i * 4 + 1] = rgbBytes[i * 3 + 1];
    bgra[i * 4 + 2] = rgbBytes[i * 3 + 0];
    bgra[i * 4 + 3] = 255;
}
var img = MLImage.CreateFromPixels(w, h, MLPixelFormat.Bgra32, bgra);
Defensive patterns

Strategy: validation

Validate before calling

if (pixelFormat != MLPixelFormat.Bgra32 && pixelFormat != MLPixelFormat.Rgba32)
    throw new ArgumentException($"Format {pixelFormat} unsupported; convert to Bgra32/Rgba32.", nameof(pixelFormat));
if (imagePixelData.Length != width * height * 4)
    throw new ArgumentException("Pixel buffer must be width*height*4 bytes.");

Type guard

bool IsCreatablePixelFormat(MLPixelFormat fmt) => fmt is MLPixelFormat.Bgra32 or MLPixelFormat.Rgba32;

Try / catch

try
{
    img = MLImage.CreateFromPixels(w, h, pixelFormat, data);
}
catch (ArgumentException ex) when (ex.ParamName == "pixelFormat")
{
    logger.LogError(ex, "Unsupported pixel format {Fmt}", pixelFormat);
}

Prevention

When it happens

Trigger: Calling MLImage.CreateFromPixels(width, height, pixelFormat, data) with formats such as Rgb24, Bgr24, Grayscale8, or a cast integer not in {Bgra32, Rgba32}.

Common situations: Receiving pixel data from camera/graphics APIs that default to RGB24 or grayscale, or from bitmaps decoded with 24bpp color depth.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/b88a8bb11f62f72d. Report an issue: GitHub.