{"record":{"id":"d0c32ffcfc99219c","repo":"dotnet/machinelearning","slug":"unsupported-image-format","errorCode":null,"errorMessage":"Unsupported image format.","messagePattern":"Unsupported image format\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":283,"sourceCode":"        private MLImage(SKBitmap image)\n        {\n            _image = image;\n\n            PixelFormat = _image.Info.ColorType switch\n            {\n                SKColorType.Bgra8888 => MLPixelFormat.Bgra32,\n                SKColorType.Rgba8888 => MLPixelFormat.Rgba32,\n                _ => CloneImageToSupportedFormat()\n            };\n        }\n\n        private MLPixelFormat CloneImageToSupportedFormat()\n        {\n            Debug.Assert(_image.Info.ColorType != SKColorType.Bgra8888 && _image.Info.ColorType != SKColorType.Rgba8888);\n\n            if (!_image.CanCopyTo(SKColorType.Bgra8888))\n            {\n                throw new InvalidOperationException(\"Unsupported image format.\");\n            }\n\n            SKBitmap image1 = _image.Copy(SKColorType.Bgra8888);\n            _image.Dispose();\n            _image = image1;\n            return MLPixelFormat.Bgra32;\n        }\n\n        internal MLImage CloneWithResizing(int width, int height, ImageResizeMode mode)\n        {\n            ThrowInvalidOperationExceptionIfDisposed();\n\n            SKBitmap image = mode switch\n            {\n                ImageResizeMode.Pad => ResizeWithPadding(width, height),\n                ImageResizeMode.Fill => ResizeFull(width, height),\n                >= ImageResizeMode.CropAnchorTop and <= ImageResizeMode.CropAnchorCentral => ResizeWithCrop(width, height, mode),\n                _ => throw new ArgumentException($\"Invalid resize mode value.\", nameof(mode))","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L265-L301","documentation":"CloneImageToSupportedFormat converts the underlying SKBitmap to Bgra8888 when the image's color type is neither Bgra8888 nor Rgba8888. If SkiaSharp reports the bitmap cannot be copied to Bgra8888 (CanCopyTo returns false), the internal invariant fails and InvalidOperationException is thrown.","triggerScenarios":"Accessing pixel/format-related APIs on an MLImage whose backing bitmap uses an exotic SKColorType that SkiaSharp cannot convert to Bgra8888 (e.g. unusual color spaces or F16/high-precision pixel formats).","commonSituations":"Images decoded from sources with HDR or non-standard pixel formats, or platform-specific decode results, later used with ML.NET image pipelines expecting 32-bit BGRA/RGBA.","solutions":["Convert the image to a standard 8-bit RGBA/BGRA format upstream (e.g. via SKBitmap.Copy with an explicit SKImageInfo of Bgra8888) before wrapping it in MLImage","Update SkiaSharp native binaries if the source format should be convertible","Catch InvalidOperationException and re-decode the source file with forced pixel format"],"exampleFix":"// before\nvar img = new MLImage(f16Bitmap); // exotic color type -> later throws\n// after\nvar converted = f16Bitmap.Copy(new SKImageInfo(f16Bitmap.Width, f16Bitmap.Height, SKColorType.Bgra8888));\nvar img = new MLImage(converted);","handlingStrategy":"try-catch","validationCode":"// ensure source decodes to 8-bit BGRA/RGBA before wrapping in MLImage","typeGuard":null,"tryCatchPattern":"try { var fmt = img.PixelFormat; } catch (InvalidOperationException ex) when (ex.Message == \"Unsupported image format.\") { var src = SKBitmap.Decode(path, new SKImageInfo(w, h, SKColorType.Bgra8888)); img = new MLImage(src); }","preventionTips":["Decode sources with an explicit Bgra8888/Rgba8888 SKImageInfo","Avoid HDR/F16 sources in ML.NET image pipelines","Use recent SkiaSharp versions with broad copy support"],"tags":["csharp","image","skia","pixel-format"],"backgroundTag":"unsupported-dtype","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"}