MonoGame/MonoGame · error · InvalidOperationException

No data set on bitmap

Error message

No data set on bitmap

What it means

Thrown by PvrtcBitmapContent.GetPixelData() when _bitmapData is null. NOTE: the field is initialized to an empty array (_bitmapData = []), so this null check is effectively dead code under normal usage — the field is never null after construction. The intended guard is against reading pixel data before SetPixelData has been called. In practice this exception is very difficult to trigger through public API because the initializer prevents null.

Source

Thrown at MonoGame.Framework.Content.Pipeline/Graphics/PvrtcBitmapContent.cs:32

    {
        private byte[] _bitmapData = [];

        private int GetDataSize()
        {
            TryGetFormat(out var format);
            return format switch
            {
                SurfaceFormat.RgbPvrtc2Bpp or SurfaceFormat.RgbaPvrtc2Bpp => (Math.Max(Width, 16) * Math.Max(Height, 8) * 2 + 7) / 8,
                SurfaceFormat.RgbPvrtc4Bpp or SurfaceFormat.RgbaPvrtc4Bpp => (Math.Max(Width, 8) * Math.Max(Height, 8) * 4 + 7) / 8,
                _ => 0,
            };
        }

        /// <inheritdoc/>
        public override byte[] GetPixelData()
        {
            if (_bitmapData == null)
                throw new InvalidOperationException("No data set on bitmap");
            var result = new byte[_bitmapData.Length];
            Buffer.BlockCopy(_bitmapData, 0, result, 0, _bitmapData.Length);
            return result;
        }

        /// <inheritdoc/>
        public override void SetPixelData(byte[] sourceData)
        {
            var size = GetDataSize();
            if (sourceData.Length != size)
                throw new ArgumentException("Incorrect data size. Expected " + size + " bytes");
            if (_bitmapData.Length != size)
                _bitmapData = new byte[size];
            Buffer.BlockCopy(sourceData, 0, _bitmapData, 0, size);
        }

        /// <inheritdoc/>
        protected override bool TryCopyFrom(BitmapContent sourceBitmap, Rectangle sourceRegion, Rectangle destinationRegion)

View on GitHub (pinned to 1d71bbd0ff)

Solutions

  1. Always call SetPixelData() before GetPixelData() on PvrtcBitmapContent instances
  2. This specific null-check path is likely unreachable given the field initializer _bitmapData = []; if you hit it, check for reflection or serialization tampering
  3. File a bug report — the check should probably test _bitmapData.Length == 0 rather than == null

Example fix

// before
var data = pvrBitmap.GetPixelData(); // may throw if no data set

// after
pvrBitmap.SetPixelData(sourceBytes);
var data = pvrBitmap.GetPixelData();
Defensive patterns

Strategy: validation

Validate before calling

// Ensure SetPixelData was called before GetPixelData.
// Note: _bitmapData is initialized to [] not null, so the null check
// in the library is effectively dead code. Check for empty data instead
// by catching the empty-result scenario at the application level.
var data = pvrBitmap.GetPixelData();
if (data.Length == 0)
    /* no pixel data has been set yet */

Try / catch

try { var data = pvrBitmap.GetPixelData(); }
catch (InvalidOperationException ex) when (ex.Message == "No data set on bitmap")
{ /* call SetPixelData first */ }

Prevention

When it happens

Trigger: Calling GetPixelData() before SetPixelData() on a PvrtcBitmapContent — though the actual null check rarely fires because _bitmapData defaults to an empty array rather than null. Could theoretically occur if the field is set to null via reflection or serialization deserialization.

Common situations: Custom PVRTC compression processors that read before writing; deserialization edge cases that leave the field null; reflection-based code that interferes with field initialization.

Related errors


AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13). Data as JSON: /api/errors/ca7378a3c31fbdd8. Report an issue: GitHub.