dotnet/wpf · error · ArgumentException

SR.Image_StreamRead

Error message

SR.Image_StreamRead

What it means

The StrokeCollection(Stream stream) constructor decodes Ink Serialized Format (ISF) from the stream. After a null check it verifies stream.CanRead; if the stream is not readable it throws ArgumentException(SR.Image_StreamRead). The stream must support reading before ISF decoding can proceed.

Solutions

  1. Open the source with read access (FileMode.Open, FileAccess.Read) before constructing
  2. Check stream.CanRead in the caller and open a new readable stream if false
  3. If the source is write-only, copy its content to a MemoryStream first and pass that

Example fix

// before
using var fs = new FileStream(path, FileMode.Append, FileAccess.Write);
var sc = new StrokeCollection(fs);
// after
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
var sc = new StrokeCollection(fs);
Defensive patterns

Strategy: validation

Validate before calling

bool readable = stream is not null && stream.CanRead;

Type guard

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

Try / catch

try { var sc = new StrokeCollection(stream); } catch (ArgumentException ex) when (ex.ParamName == "stream") { /* open a readable stream and retry */ }

Prevention

When it happens

Trigger: Passing a write-only stream (e.g. opened with FileAccess.Write) or a closed stream to the StrokeCollection(Stream) constructor.

Common situations: Loading ink from a FileStream opened with wrong FileMode/FileAccess; passing a response/request body stream positioned for writing; using a disposed or closed stream.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

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

            {
                if ( items.Contains(stroke) )
                {
                    //clear and throw
                    items.Clear();
                    throw new ArgumentException(SR.StrokeIsDuplicated, nameof(strokes));
                }
                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>

View on GitHub (pinned to 81131a70a4)