dotnet/machinelearning · error · InvalidOperationException

Unsupported image format.

Error message

Unsupported image format.

What it means

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.

Source

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

        private MLImage(SKBitmap image)
        {
            _image = image;

            PixelFormat = _image.Info.ColorType switch
            {
                SKColorType.Bgra8888 => MLPixelFormat.Bgra32,
                SKColorType.Rgba8888 => MLPixelFormat.Rgba32,
                _ => CloneImageToSupportedFormat()
            };
        }

        private MLPixelFormat CloneImageToSupportedFormat()
        {
            Debug.Assert(_image.Info.ColorType != SKColorType.Bgra8888 && _image.Info.ColorType != SKColorType.Rgba8888);

            if (!_image.CanCopyTo(SKColorType.Bgra8888))
            {
                throw new InvalidOperationException("Unsupported image format.");
            }

            SKBitmap image1 = _image.Copy(SKColorType.Bgra8888);
            _image.Dispose();
            _image = image1;
            return MLPixelFormat.Bgra32;
        }

        internal MLImage CloneWithResizing(int width, int height, ImageResizeMode mode)
        {
            ThrowInvalidOperationExceptionIfDisposed();

            SKBitmap image = mode switch
            {
                ImageResizeMode.Pad => ResizeWithPadding(width, height),
                ImageResizeMode.Fill => ResizeFull(width, height),
                >= ImageResizeMode.CropAnchorTop and <= ImageResizeMode.CropAnchorCentral => ResizeWithCrop(width, height, mode),
                _ => throw new ArgumentException($"Invalid resize mode value.", nameof(mode))

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. 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
  2. Update SkiaSharp native binaries if the source format should be convertible
  3. Catch InvalidOperationException and re-decode the source file with forced pixel format

Example fix

// before
var img = new MLImage(f16Bitmap); // exotic color type -> later throws
// after
var converted = f16Bitmap.Copy(new SKImageInfo(f16Bitmap.Width, f16Bitmap.Height, SKColorType.Bgra8888));
var img = new MLImage(converted);
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure source decodes to 8-bit BGRA/RGBA before wrapping in MLImage

Try / catch

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); }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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