dotnet/wpf · error · ArgumentException
SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo…
Error message
SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo, MaxGlyphCount)
What it means
GlyphRun.Initialize caps the number of glyphs at MaxGlyphCount because very large runs can overflow native resources; exceeding it throws ArgumentException stating the collection must have at most MaxGlyphCount elements, naming glyphIndices.
Solutions
- Split the text into smaller runs (per line or per paragraph) each under MaxGlyphCount
- Use TextFormatter/TextSource-based layout which chunks runs naturally
- Check glyphIndices.Count <= MaxGlyphCount before construction and paginate
Example fix
// before: one run for 100k glyphs
// after
foreach (var chunk in ChunkGlyphs(glyphs, MaxGlyphCount))
dc.DrawGlyphRun(foreground, MakeRun(chunk)); Defensive patterns
Strategy: validation
Validate before calling
if (glyphIndices.Count > GlyphRun.MaxGlyphCount)
glyphIndices = SplitIntoChunks(glyphIndices, GlyphRun.MaxGlyphCount); // draw in batches Type guard
static bool WithinMaxGlyphs(ICollection<ushort> glyphs) => glyphs != null && glyphs.Count <= GlyphRun.MaxGlyphCount;
Try / catch
try { var run = new GlyphRun(...); }
catch (ArgumentException ex) when (ex.ParamName == "glyphIndices" && ex.Message.Contains("less or equal")) { /* split and redraw */ } Prevention
- Chunk large glyph lists per line or paragraph
- Prefer TextFormatter-based layout which chunks automatically
- Check counts against MaxGlyphCount before construction
When it happens
Trigger: Calling the GlyphRun constructor, TryCreate, or EndInit with glyphIndices.Count > MaxGlyphCount — e.g. shaping an entire large document into a single run instead of per-line/per-paragraph runs.
Common situations: Batch-rendering thousands of glyphs in one run in custom text-layout code; caching a whole page of shaped text as one GlyphRun.
Understand the failure class
Background: "File too large" / "file size exceeds limit" errors: why libraries cap file sizes and how to fix them — this error's family across 46 libraries.
Related errors
- SR.ClusterMapEntriesShouldNotDecrease
- SR.ClusterMapEntryShouldPointWithinGlyphIndices
- SR.ClusterMapFirstEntryMustBeZero
- SR.CollectionNumberOfElementsMustBeGreaterThanZero
- SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/311cc9d39b8e4fe8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs:437
// 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);
}
IsInitialized = true; // The glyphrun is completely initialized
}
#endregion ConstructorsView on GitHub (pinned to 81131a70a4)