dotnet/wpf · error · ArgumentException

SR.Invalid_isfData_Length

Error message

SR.Invalid_isfData_Length

What it means

After confirming readability, the StrokeCollection(Stream) constructor calls GetSeekableStream; ISF decoding requires a seekable stream, and when none can be obtained it throws ArgumentException(SR.Invalid_isfData_Length). The stream data must be seekable and correspond to valid ISF data of decodable length.

Solutions

  1. Copy the stream into a MemoryStream before constructing: new StrokeCollection(new MemoryStream(stream bytes))
  2. Verify the source really is ISF data (written via StrokeCollection.Save)
  3. Check the stream length/content for truncation before decoding

Example fix

// before
var sc = new StrokeCollection(networkStream);
// after
using var ms = new MemoryStream();
networkStream.CopyTo(ms);
ms.Position = 0;
var sc = new StrokeCollection(ms);
Defensive patterns

Strategy: try-catch

Validate before calling

bool usable = stream is not null && stream.CanRead && (stream.CanSeek || true); // prefer: buffer into MemoryStream when !stream.CanSeek

Type guard

static bool IsSeekable(Stream? s) => s is not null && s.CanRead && s.CanSeek;

Try / catch

try { var sc = new StrokeCollection(stream); } catch (ArgumentException ex) when (ex.ParamName == "stream") { using var ms = new MemoryStream(); stream.CopyTo(ms); ms.Position = 0; var sc = new StrokeCollection(ms); }

Prevention

When it happens

Trigger: Passing a non-seekable stream (e.g. NetworkStream, GZipStream wrapper, or a forward-only pipe) whose content cannot be buffered into a seekable stream; passing a stream with truncated/invalid ISF data.

Common situations: Loading ink directly from an HTTP response stream or socket stream; ISF files that were truncated by an interrupted write; deserializing non-ISF data saved by another tool.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection.cs:66

                }
                items.Add(stroke);
            }
        }

        /// <summary>Creates a collection from ISF data in the specified stream</summary>
        /// <param name="stream">Stream of ISF data</param>
        public StrokeCollection(Stream stream)
        {
            ArgumentNullException.ThrowIfNull(stream);
            if ( !stream.CanRead )
            {
                throw new ArgumentException(SR.Image_StreamRead, nameof(stream));
            }

            Stream seekableStream = GetSeekableStream(stream);
            if (seekableStream == null)
            {
                throw new ArgumentException(SR.Invalid_isfData_Length, nameof(stream));
            }

            //this will init our stroke collection
            StrokeCollectionSerializer serializer = new StrokeCollectionSerializer(this);
            serializer.DecodeISF(seekableStream);
        }


        /// <summary>Save the collection of strokes, including any custom attributes to a stream</summary>
        /// <param name="stream">The stream to save Ink Serialized Format to</param>
        /// <param name="compress">Flag if set to true the data will be compressed, which can
        /// reduce the output buffer size in exchange for slower Save performance.</param>
        public virtual void Save(Stream stream, bool compress)
        {
            ArgumentNullException.ThrowIfNull(stream);
            if ( !stream.CanWrite )
            {
                throw new ArgumentException(SR.Image_StreamWrite, nameof(stream));

View on GitHub (pinned to 81131a70a4)