{"record":{"id":"2e95bbefd5862dc4","repo":"dotnet/machinelearning","slug":"image-pixel-format-is-not-supported","errorCode":null,"errorMessage":"Image pixel format is not supported","messagePattern":"Image pixel format is not supported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/ImagePixelExtractor.cs","lineNumber":370,"sourceCode":"                        float offset = ex.OffsetImage;\n                        float scale = ex.ScaleImage;\n                        Contracts.Assert(scale != 0);\n\n                        // REVIEW: split the getter into 2 specialized getters, one for float case and one for byte case.\n                        Span<float> vf = typeof(TValue) == typeof(float) ? MemoryMarshal.Cast<TValue, float>(editor.Values) : default;\n                        Span<byte> vb = typeof(TValue) == typeof(byte) ? MemoryMarshal.Cast<TValue, byte>(editor.Values) : default;\n                        Contracts.Assert(!vf.IsEmpty || !vb.IsEmpty);\n                        bool needScale = offset != 0 || scale != 1;\n                        Contracts.Assert(!needScale || !vf.IsEmpty);\n\n                        ImagePixelExtractingEstimator.GetOrder(ex.OrderOfExtraction, ex.ColorsToExtract, out int a, out int r, out int b, out int g);\n\n                        ReadOnlySpan<byte> pixelData = src.Pixels;\n                        (int alphaIndex, int redIndex, int greenIndex, int blueIndex) = src.PixelFormat switch\n                        {\n                            MLPixelFormat.Bgra32 => (3, 2, 1, 0),\n                            MLPixelFormat.Rgba32 => (3, 0, 1, 2),\n                            _ => throw new InvalidOperationException($\"Image pixel format is not supported\")\n                        };\n\n                        int h = height;\n                        int w = width;\n                        int pixelByteCount = alphaIndex > 0 ? 4 : 3;\n                        int ix = 0;\n\n                        if (ex.InterleavePixelColors)\n                        {\n                            int idst = 0;\n                            for (int y = 0; y < h; ++y)\n                            {\n                                for (int x = 0; x < w; x++)\n                                {\n                                    if (!vb.IsEmpty)\n                                    {\n                                        if (a != -1) { vb[idst + a] = (byte)(alphaIndex > 0 ? pixelData[ix + alphaIndex] : 255); }\n                                        if (r != -1) { vb[idst + r] = pixelData[ix + redIndex]; }","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/ImagePixelExtractor.cs#L352-L388","documentation":"ImagePixelExtractor converts an already-decoded image's raw pixels into channel planes. It only knows how to index bytes for Bgra32 and Rgba32 pixel formats; any other MLPixelFormat falls through the switch's discard arm and raises an InvalidOperationException because the byte offsets for alpha/red/green/blue are undefined for that format.","triggerScenarios":"A decoded MLImage/Image whose PixelFormat is neither MLPixelFormat.Bgra32 nor MLPixelFormat.Rgba32 (e.g. Grayscale8, Bgr24, indexed formats) flowing into the pixel extraction stage of an image-processing pipeline.","commonSituations":"Loading grayscale or palette images (PNG-8, 16-bit PNG, JPEG gray) from disk or a custom decoder, or images produced by another component that returns a non-32bpp format.","solutions":["Convert the image to Rgba32 or Bgra32 before extraction (e.g. re-encode/decode via SKBitmap with SKColorType.Bgra8888)","Check src.PixelFormat ahead of the pipeline and normalize all inputs to a supported 32bpp format at load time","Upgrade ML.NET / Microsoft.ML.ImageAnalytics — newer versions may support additional formats"],"exampleFix":"// before\nvar img = MLImage.CreateFromFile(path); // may be Grayscale8 etc.\n// after\nvar img = MLImage.CreateFromFile(path);\nif (img.PixelFormat != MLPixelFormat.Bgra32 && img.PixelFormat != MLPixelFormat.Rgba32)\n    img = ConvertToBgra32(img); // re-encode via SkiaSharp to SKColorType.Bgra8888","handlingStrategy":"type-guard","validationCode":"if (img.PixelFormat != MLPixelFormat.Bgra32 && img.PixelFormat != MLPixelFormat.Rgba32)\n    img = ConvertToBgra32(img);","typeGuard":"bool IsSupportedPixelFormat(MLPixelFormat fmt) => fmt is MLPixelFormat.Bgra32 or MLPixelFormat.Rgba32;","tryCatchPattern":"try\n{\n    ExtractPixels(img);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"pixel format\"))\n{\n    img = ConvertToBgra32(img);\n    ExtractPixels(img);\n}","preventionTips":["Normalize all images to 32bpp (Bgra32) at ingestion","Check PixelFormat right after decode, before any extraction stage","Convert grayscale/palette images at load time rather than mid-pipeline"],"tags":["csharp","images","pixel-format","unsupported"],"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-14T05:17:10.506Z"}