dotnet/wpf · error · ArgumentException

SR.CollectionNumberOfElementsMustBeGreaterThanZero

Error message

SR.CollectionNumberOfElementsMustBeGreaterThanZero

What it means

GlyphRun.Initialize requires the glyphIndices collection to contain at least one glyph; an empty collection cannot represent a drawable run. It throws ArgumentException naming the glyphIndices parameter.

Solutions

  1. Return early / skip GlyphRun creation when there are no glyphs
  2. Pass null-safe handling: only construct the run if glyphIndices.Count > 0
  3. Render an empty visual (e.g. empty DrawingContext) instead of an empty run

Example fix

// before
var run = new GlyphRun(tf, 0, false, 12, glyphs /* empty */, ...);
// after
if (glyphs.Count > 0) { var run = new GlyphRun(...); dc.DrawText(...); }
Defensive patterns

Strategy: validation

Validate before calling

if (glyphIndices == null || glyphIndices.Count == 0)
    return; // skip drawing instead of constructing a GlyphRun

Type guard

static bool HasGlyphs(ICollection<ushort> glyphs) => glyphs != null && glyphs.Count > 0;

Try / catch

try { var run = new GlyphRun(...); }
catch (ArgumentException ex) when (ex.ParamName == "glyphIndices") { /* skip empty run */ }

Prevention

When it happens

Trigger: Calling the GlyphRun constructor, TryCreate, or EndInit with an empty (Count == 0) glyphIndices list — e.g. shaping an empty string but still constructing a run.

Common situations: Shaping empty or whitespace-only text that yields zero glyphs; clearing a text buffer and unconditionally building a GlyphRun.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs:433

                    throw new ArgumentException(SR.SidewaysRTLTextIsNotSupported);

                // NOTE:  In previous versions this function would estimate the size
                // of this glyph run's bitmaps and compare it against the theoretical
                // maximum size allowed before rendering falls back to using geometry.
                // This was done in order to produce a managed exception where we might
                // hit overflow or memory allocation issues in native code.
                // We no longer own the code the produces these bitmaps so we can't reliably
                // avoid the issue here any longer.
            }
            else
            {
                ArgumentOutOfRangeException.ThrowIfEqual(renderingEmSize, double.NaN);
                ArgumentOutOfRangeException.ThrowIfNegative(renderingEmSize);
                ArgumentNullException.ThrowIfNull(glyphTypeface);
                ArgumentNullException.ThrowIfNull(glyphIndices);

                if (glyphIndices.Count <= 0)
                    throw new ArgumentException(SR.CollectionNumberOfElementsMustBeGreaterThanZero, nameof(glyphIndices));

                if (glyphIndices.Count > MaxGlyphCount)
                {
                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo, MaxGlyphCount), nameof(glyphIndices));
                }

                ArgumentNullException.ThrowIfNull(advanceWidths);

                if (advanceWidths.Count != glyphIndices.Count)
                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count), nameof(advanceWidths));

                if (glyphOffsets != null && glyphOffsets.Count != 0 && glyphOffsets.Count != glyphIndices.Count)
                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count), nameof(glyphOffsets));

                // We should've caught all invalid cases above and thrown appropriate exceptions.
                Invariant.Assert(false);
            }

View on GitHub (pinned to 81131a70a4)