dotnet/wpf · error · ArgumentException

Read different size from stream then expected

Error message

Read different size from stream then expected

What it means

LoadStrokeIds reads the expected number of stroke-ID bytes with StrokeCollectionSerializer.ReliableRead and throws ArgumentException when the bytes actually read differ from the expected count. The stream ended early or returned fewer bytes, so the ISF payload is truncated or inconsistent.

Solutions

  1. Ensure the stream delivers the full ISF payload (read fully into a MemoryStream first)
  2. Verify file integrity (size, checksum) before loading
  3. Re-save the ink data from the original source
  4. Catch ArgumentException and recover with an empty StrokeCollection

Example fix

// before
var strokes = new StrokeCollection(netStream);
// after
var ms = new MemoryStream();
netStream.CopyTo(ms); ms.Position = 0;
try { var strokes = new StrokeCollection(ms); }
catch (ArgumentException) { strokes = new StrokeCollection(); }
Defensive patterns

Strategy: fallback

Validate before calling

// buffer the full payload so ReliableRead never sees a short stream
var ms = new MemoryStream(); source.CopyTo(ms); ms.Position = 0;

Type guard

bool FullyBuffered(Stream s) => s is MemoryStream || (s.CanSeek && s.Length > 0);

Try / catch

try { strokes = new StrokeCollection(ms); }
catch (ArgumentException) { strokes = FallbackInk(); }

Prevention

When it happens

Trigger: DecodeRawISF on a stream where ReliableRead returns fewer bytes than the declared stroke-ID block size.

Common situations: Reading from a network/pipe stream that delivered partial data, truncated .isf files, wrong offsets when slicing ISF out of a container format.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/98d127e7fbbdb920. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/InkSerializer.cs:222

            uint count;

            cb = SerializationHelper.Decode(isfStream, out count);
            if (cb > cbTotal)
                throw new ArgumentException(ISFDebugMessage("Invalid ISF data"), nameof(isfStream));

            cbTotal -= cb;
            if (0 == count)
                return (cbSize - cbTotal);

            cb = cbTotal;

            byte[] inputdata = new byte[cb];

            // read the stream
            uint bytesRead = StrokeCollectionSerializer.ReliableRead(isfStream, inputdata, cb);
            if (cb != bytesRead)
            {
                throw new ArgumentException(StrokeCollectionSerializer.ISFDebugMessage("Read different size from stream then expected"), nameof(isfStream));
            }
            cbTotal -= cb;

            if (0 != cbTotal)
                throw new ArgumentException(ISFDebugMessage("Invalid ISF data"), nameof(isfStream));

            return cbSize;
        }


        private bool IsGIFData(Stream inkdata)
        {
            Debug.Assert(inkdata != null);
            long currentPosition = inkdata.Position;
            try
            {
                return ((byte)inkdata.ReadByte() == 'G' &&
                        (byte)inkdata.ReadByte() == 'I' &&

View on GitHub (pinned to 81131a70a4)