AvaloniaUI/Avalonia · error · InvalidOperationException

Transform is not invertible.

Error message

Transform is not invertible.

What it means

Matrix.Invert throws when the matrix is singular (determinant is zero or near-zero), meaning it has no inverse. Matrix transforms map points; a non-invertible matrix collapses space (e.g. a zero-scale transform) and cannot reverse-map points. Use TryInvert for a non-throwing variant.

Source

Thrown at src/Avalonia.Base/Matrix.cs:490

                (_m21 * _m13 - _m11 * _m23) * invdet,
                (_m21 * _m32 - _m31 * _m22) * invdet,
                (_m31 * _m12 - _m11 * _m32) * invdet,
                (_m11 * _m22 - _m21 * _m12) * invdet
                );
            
            return true;
        }

        /// <summary>
        /// Inverts the Matrix.
        /// </summary>
        /// <exception cref="InvalidOperationException">Matrix is not invertible.</exception>
        /// <returns>The inverted matrix.</returns>
        public Matrix Invert()
        {
            if (!TryInvert(out var inverted))
            {
                throw new InvalidOperationException("Transform is not invertible.");
            }

            return inverted;
        }

        /// <summary>
        /// Parses a <see cref="Matrix"/> string.
        /// </summary>
        /// <param name="s">Six or nine comma-delimited double values (m11, m12, m21, m22, offsetX, offsetY[, perspX, perspY, perspZ]) that describe the new <see cref="Matrix"/></param>
        /// <returns>The <see cref="Matrix"/>.</returns>
        public static Matrix Parse(string s)
        {
            // initialize to satisfy compiler - only used when retrieved from string.
            double v8 = 0;
            double v9 = 0;

            using (var tokenizer = new SpanStringTokenizer(s, CultureInfo.InvariantCulture, exceptionMessage: "Invalid Matrix."))
            {

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use matrix.TryInvert(out var inv) instead of Invert() and handle the false case gracefully.
  2. Ensure scale transforms never reach exactly 0 on a collapsed axis — clamp to a small epsilon.
  3. Check matrix.HasInverse before calling Invert() if available, or compute the determinant.

Example fix

// before
var inv = transform.Value.Invert();

// after
if (transform.Value.TryInvert(out var inv))
{
    // use inv
}
else
{
    // handle non-invertible case (e.g. skip hit test)
}
Defensive patterns

Strategy: validation

Validate before calling

if (matrix.TryInvert(out var inv))
{
    // use inv
}
else
{
    // matrix is singular; skip invert-dependent logic
}

Try / catch

Matrix inv;
try { inv = matrix.Invert(); }
catch (InvalidOperationException)
{
    // handle non-invertible: skip hit-test or use identity
    inv = Matrix.Identity;
}

Prevention

When it happens

Trigger: Calling matrix.Invert() on a matrix with determinant 0 — typically a scale of (0, anything) or (anything, 0), or any transform that collapses a dimension. Also from rendering code that needs to invert a transform for hit-testing.

Common situations: Setting a ScaleTransform with scale 0. A composite transform that mathematically collapses. Animating a scale property through 0. Hit-testing code inverting a degenerate transform.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/dcbca462492f9d96. Report an issue: GitHub.