{"record":{"id":"311cc9d39b8e4fe8","repo":"dotnet/wpf","slug":"sr-format-sr-collectionnumberofelementsmustbelessorequalto","errorCode":null,"errorMessage":"SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo, MaxGlyphCount)","messagePattern":"SR\\.Format\\(SR\\.CollectionNumberOfElementsMustBeLessOrEqualTo, MaxGlyphCount\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs","lineNumber":437,"sourceCode":"                // maximum size allowed before rendering falls back to using geometry.\n                // This was done in order to produce a managed exception where we might\n                // hit overflow or memory allocation issues in native code.\n                // We no longer own the code the produces these bitmaps so we can't reliably\n                // avoid the issue here any longer.\n            }\n            else\n            {\n                ArgumentOutOfRangeException.ThrowIfEqual(renderingEmSize, double.NaN);\n                ArgumentOutOfRangeException.ThrowIfNegative(renderingEmSize);\n                ArgumentNullException.ThrowIfNull(glyphTypeface);\n                ArgumentNullException.ThrowIfNull(glyphIndices);\n\n                if (glyphIndices.Count <= 0)\n                    throw new ArgumentException(SR.CollectionNumberOfElementsMustBeGreaterThanZero, nameof(glyphIndices));\n\n                if (glyphIndices.Count > MaxGlyphCount)\n                {\n                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo, MaxGlyphCount), nameof(glyphIndices));\n                }\n\n                ArgumentNullException.ThrowIfNull(advanceWidths);\n\n                if (advanceWidths.Count != glyphIndices.Count)\n                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count), nameof(advanceWidths));\n\n                if (glyphOffsets != null && glyphOffsets.Count != 0 && glyphOffsets.Count != glyphIndices.Count)\n                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count), nameof(glyphOffsets));\n\n                // We should've caught all invalid cases above and thrown appropriate exceptions.\n                Invariant.Assert(false);\n            }\n\n            IsInitialized = true; // The glyphrun is completely initialized\n        }\n\n        #endregion Constructors","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs#L419-L455","documentation":"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.","triggerScenarios":"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.","commonSituations":"Batch-rendering thousands of glyphs in one run in custom text-layout code; caching a whole page of shaped text as one GlyphRun.","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"],"exampleFix":"// before: one run for 100k glyphs\n// after\nforeach (var chunk in ChunkGlyphs(glyphs, MaxGlyphCount))\n    dc.DrawGlyphRun(foreground, MakeRun(chunk));","handlingStrategy":"validation","validationCode":"if (glyphIndices.Count > GlyphRun.MaxGlyphCount)\n    glyphIndices = SplitIntoChunks(glyphIndices, GlyphRun.MaxGlyphCount); // draw in batches","typeGuard":"static bool WithinMaxGlyphs(ICollection<ushort> glyphs) => glyphs != null && glyphs.Count <= GlyphRun.MaxGlyphCount;","tryCatchPattern":"try { var run = new GlyphRun(...); }\ncatch (ArgumentException ex) when (ex.ParamName == \"glyphIndices\" && ex.Message.Contains(\"less or equal\")) { /* split and redraw */ }","preventionTips":["Chunk large glyph lists per line or paragraph","Prefer TextFormatter-based layout which chunks automatically","Check counts against MaxGlyphCount before construction"],"tags":["wpf","glyphrun","size-limit","argument-validation"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}