{"record":{"id":"b88a8bb11f62f72d","repo":"dotnet/machinelearning","slug":"unsupported-pixel-format","errorCode":null,"errorMessage":"Unsupported pixel format","messagePattern":"Unsupported pixel format","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":85,"sourceCode":"                throw new ArgumentException($\"Invalid path\", nameof(imagePath));\n            }\n\n            return new MLImage(image);\n        }\n\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));","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L67-L103","documentation":"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.","triggerScenarios":"Calling MLImage.CreateFromPixels(width, height, pixelFormat, data) with formats such as Rgb24, Bgr24, Grayscale8, or a cast integer not in {Bgra32, Rgba32}.","commonSituations":"Receiving pixel data from camera/graphics APIs that default to RGB24 or grayscale, or from bitmaps decoded with 24bpp color depth.","solutions":["Convert the buffer to Bgra32 or Rgba32 (add an alpha byte, expanding 3bpp to 4bpp) before calling","Confirm the source reports its own format instead of assuming; map it explicitly to Bgra32/Rgba32","Use MLImage.CreateFromStream/CreateFromFile for non-32bpp sources so Skia converts internally"],"exampleFix":"// before\nvar img = MLImage.CreateFromPixels(w, h, MLPixelFormat.Rgb24, rgbBytes); // throws\n// after\nbyte[] bgra = new byte[w * h * 4];\nfor (int i = 0; i < w * h; i++)\n{\n    bgra[i * 4 + 0] = rgbBytes[i * 3 + 2];\n    bgra[i * 4 + 1] = rgbBytes[i * 3 + 1];\n    bgra[i * 4 + 2] = rgbBytes[i * 3 + 0];\n    bgra[i * 4 + 3] = 255;\n}\nvar img = MLImage.CreateFromPixels(w, h, MLPixelFormat.Bgra32, bgra);","handlingStrategy":"validation","validationCode":"if (pixelFormat != MLPixelFormat.Bgra32 && pixelFormat != MLPixelFormat.Rgba32)\n    throw new ArgumentException($\"Format {pixelFormat} unsupported; convert to Bgra32/Rgba32.\", nameof(pixelFormat));\nif (imagePixelData.Length != width * height * 4)\n    throw new ArgumentException(\"Pixel buffer must be width*height*4 bytes.\");","typeGuard":"bool IsCreatablePixelFormat(MLPixelFormat fmt) => fmt is MLPixelFormat.Bgra32 or MLPixelFormat.Rgba32;","tryCatchPattern":"try\n{\n    img = MLImage.CreateFromPixels(w, h, pixelFormat, data);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"pixelFormat\")\n{\n    logger.LogError(ex, \"Unsupported pixel format {Fmt}\", pixelFormat);\n}","preventionTips":["Standardize internal buffers on Bgra32","Map camera/decoder formats to Bgra32/Rgba32 at the boundary","Check buffer length = width*height*4 before calling"],"tags":["csharp","images","pixel-format","argument-validation"],"backgroundTag":"unsupported-enum-value","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}