{"record":{"id":"086da2be5f966718","repo":"dotnet/machinelearning","slug":"pixel-data-is-unavailable","errorCode":null,"errorMessage":"Pixel data is unavailable.","messagePattern":"Pixel data is unavailable\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":167,"sourceCode":"                }\n\n                return pixels;\n            }\n        }\n\n        /// <summary>\n        /// Gets the image pixel data.\n        /// </summary>\n        public unsafe ReadOnlySpan<byte> Pixels\n        {\n            get\n            {\n                ThrowInvalidOperationExceptionIfDisposed();\n                Debug.Assert(_image.Info.BytesPerPixel == 4);\n\n                var pixelsPtr = _image.GetPixels();\n                if (pixelsPtr == IntPtr.Zero || _image.ByteCount <= 0)\n                    throw new InvalidOperationException(\"Pixel data is unavailable.\");\n\n                return new ReadOnlySpan<byte>(pixelsPtr.ToPointer(), _image.ByteCount);\n            }\n        }\n\n        /// <summary>\n        /// Gets or sets the image tag.\n        /// </summary>\n        public string Tag\n        {\n            get\n            {\n                ThrowInvalidOperationExceptionIfDisposed();\n                return _tag;\n            }\n\n            set\n            {","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L149-L185","documentation":"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.","triggerScenarios":"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.","commonSituations":"Reading Pixels on images created from encoded streams that failed to decode fully, or on an image whose backing bitmap was never materialized.","solutions":["Check the image loaded/decoded successfully before accessing Pixels","Call GetPixels()/Pixels only on images created via CreateFromPixels or successfully loaded files","Wrap access in try-catch for InvalidOperationException and fall back to reloading the image"],"exampleFix":"// before\nvar pixels = image.Pixels;\n// after\nReadOnlySpan<byte> pixels;\ntry { pixels = image.Pixels; } catch (InvalidOperationException) { pixels = ReadOnlySpan<byte>.Empty; }","handlingStrategy":"try-catch","validationCode":"if (image == null) throw new InvalidOperationException(\"image not loaded\");","typeGuard":"static bool HasPixels(MLImage img) => img is not null; // pointer state not publicly checkable","tryCatchPattern":"try { var px = image.Pixels; } catch (InvalidOperationException) { image = MLImage.CreateFromFile(path); var px = image.Pixels; }","preventionTips":["Only read Pixels from images known to have decoded successfully","Reload the image if pixel access fails","Avoid caching MLImage references across long lifetimes"],"tags":["csharp","image","invalid-state"],"backgroundTag":"internal-invariant-violation","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"}