dotnet/wpf · error · ArgumentException

InvalidMatrixContainsInfinity

Error message

InvalidMatrixContainsInfinity

What it means

Same validation path as NaN: a Matrix-valued StylusTipTransform containing Infinity in any coefficient throws ArgumentException SR.InvalidMatrixContainsInfinity, because infinite values cannot be serialized into ink data.

Solutions

  1. Validate all six coefficients are finite (double.IsInfinity) before assigning
  2. Recompute the transform with guarded division / clamping
  3. Catch ArgumentException and substitute Matrix.Identity

Example fix

// before
attrs[KnownIds.StylusTipTransform] = inv; // may contain Infinity
// after
if (inv.HasInverse && IsFinite(inv)) attrs[KnownIds.StylusTipTransform] = inv;
static bool IsFinite(Matrix 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));
Defensive patterns

Strategy: validation

Validate before calling

static bool ContainsInfinity(Matrix 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);
if (ContainsInfinity(matrix)) matrix = Matrix.Identity;

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Assigning a Matrix with infinite coefficients (e.g. from division by a zero determinant when inverting, or unbounded math) to StylusTipTransform, then serializing/validating.

Common situations: Inverting a near-singular matrix producing huge/infinite values; zoom math dividing by zero scale; overflow in accumulated transforms.

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/23d6cf720063ed65. Report an issue: GitHub.

Appendix: source

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

                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) )
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType, typeof(double)), nameof(value));
                }

                double dVal = (double)value;

View on GitHub (pinned to 81131a70a4)