dotnet/wpf · error · ArgumentException

InvalidMatrixContainsNaN

Error message

InvalidMatrixContainsNaN

What it means

Validation error in the ink (ISF) custom-attribute serializer when the value stored for StylusTipTransform is neither a String nor a Matrix, or the Matrix is invalid (e.g. contains NaN / has no inverse); the sentinel names the failing shape check during serialization of StylusTipTransform.

Solutions

  1. Check MatrixHelper-style NaN before assigning (compare coefficients via Matrix decodes / double.IsNaN on components)
  2. Recompute the matrix from valid source values
  3. Catch ArgumentException and reset the property to Matrix.Identity

Example fix

// before
attrs[KnownIds.StylusTipTransform] = computedMatrix;
// after
if (!HasNaN(computedMatrix)) attrs[KnownIds.StylusTipTransform] = computedMatrix;
static bool HasNaN(Matrix m) => double.IsNaN(m.M11) || double.IsNaN(m.M12) || double.IsNaN(m.M21) || double.IsNaN(m.M22) || double.IsNaN(m.OffsetX) || double.IsNaN(m.OffsetY);
Defensive patterns

Strategy: validation

Validate before calling

static bool ContainsNaN(Matrix m) => double.IsNaN(m.M11)||double.IsNaN(m.M12)||double.IsNaN(m.M21)||double.IsNaN(m.M22)||double.IsNaN(m.OffsetX)||double.IsNaN(m.OffsetY);
if (ContainsNaN(matrix)) matrix = Matrix.Identity;

Type guard

static bool IsFinite(Matrix m) => !(ContainsNaN(m) || double.IsInfinity(m.M11)||double.IsInfinity(m.M12)||double.IsInfinity(m.M21)||double.IsInfinity(m.M22)||double.IsInfinity(m.OffsetX)||double.IsInfinity(m.OffsetY));

Try / catch

try { serializer.Serialize(...); }
catch (ArgumentException ex) when (ex.Message.Contains("NaN")) { props[KnownIds.StylusTipTransform] = Matrix.Identity; }

Prevention

When it happens

Trigger: Assigning a Matrix with NaN in any of its six coefficients (from uninitialized doubles, 0.0/0.0 computations, or NaN origin offsets) to StylusTipTransform and serializing.

Common situations: Matrix built from NaN layout results or division by zero in transform math; data read from corrupt external sources.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/CustomAttributeSerializer.cs:833

            {
                //
                // StylusTipTransform gets serialized as a String, but at runtime is a Matrix
                //
                Type t = value.GetType();
                if ( t != typeof(String) && t != typeof(Matrix) )
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType1, typeof(String), typeof(Matrix)), nameof(value));
                }
                else if ( t == typeof(Matrix) )
                {
                    Matrix matrix = (Matrix)value;
                    if ( !matrix.HasInverse )
                    {
                        throw new ArgumentException(SR.MatrixNotInvertible, nameof(value));
                    }
                    if ( MatrixHelper.ContainsNaN(matrix))
                    {
                        throw new ArgumentException(SR.InvalidMatrixContainsNaN, nameof(value));
                    }
                    if ( MatrixHelper.ContainsInfinity(matrix))
                    {
                        throw new ArgumentException(SR.InvalidMatrixContainsInfinity, nameof(value));
                    }
}
            }
            else if (id == KnownIds.IsHighlighter)
            {
                if ( value.GetType() != typeof(bool))
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType, typeof(bool)), nameof(value));
                }
            }
            else if ( id == KnownIds.StylusHeight || id == KnownIds.StylusWidth )
            {
                if ( value.GetType() != typeof(double) )
                {

View on GitHub (pinned to 81131a70a4)