dotnet/machinelearning · error · InvalidOperationException

Pixel data is unavailable.

Error message

Pixel data is unavailable.

What it means

The Pixels property returns a read-only span over the underlying SKBitmap's pixel buffer. If SkiaSharp returns a null pointer or reports zero bytes, the pixel data cannot be safely exposed, so InvalidOperationException is thrown.

Source

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

                }

                return pixels;
            }
        }

        /// <summary>
        /// Gets the image pixel data.
        /// </summary>
        public unsafe ReadOnlySpan<byte> Pixels
        {
            get
            {
                ThrowInvalidOperationExceptionIfDisposed();
                Debug.Assert(_image.Info.BytesPerPixel == 4);

                var pixelsPtr = _image.GetPixels();
                if (pixelsPtr == IntPtr.Zero || _image.ByteCount <= 0)
                    throw new InvalidOperationException("Pixel data is unavailable.");

                return new ReadOnlySpan<byte>(pixelsPtr.ToPointer(), _image.ByteCount);
            }
        }

        /// <summary>
        /// Gets or sets the image tag.
        /// </summary>
        public string Tag
        {
            get
            {
                ThrowInvalidOperationExceptionIfDisposed();
                return _tag;
            }

            set
            {

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Check the image loaded/decoded successfully before accessing Pixels
  2. Call GetPixels()/Pixels only on images created via CreateFromPixels or successfully loaded files
  3. Wrap access in try-catch for InvalidOperationException and fall back to reloading the image

Example fix

// before
var pixels = image.Pixels;
// after
ReadOnlySpan<byte> pixels;
try { pixels = image.Pixels; } catch (InvalidOperationException) { pixels = ReadOnlySpan<byte>.Empty; }
Defensive patterns

Strategy: try-catch

Validate before calling

if (image == null) throw new InvalidOperationException("image not loaded");

Type guard

static bool HasPixels(MLImage img) => img is not null; // pointer state not publicly checkable

Try / catch

try { var px = image.Pixels; } catch (InvalidOperationException) { image = MLImage.CreateFromFile(path); var px = image.Pixels; }

Prevention

When it happens

Trigger: Accessing the Pixels property on an MLImage whose underlying SKBitmap has no allocated pixel memory — e.g. an image created in a way that deferred/allocation failed, or after internal state became invalid.

Common situations: Reading Pixels on images created from encoded streams that failed to decode fully, or on an image whose backing bitmap was never materialized.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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