dotnet/wpf · error · InvalidOperationException

SR.MaximumNoteSizeExceeded

Error message

SR.MaximumNoteSizeExceeded

What it means

When saving a text StickyNote, the RichTextBox XAML range is serialized to a MemoryStream; if the serialized XAML exceeds MaxBufferSize, Save throws InvalidOperationException('MaximumNoteSizeExceeded'). WPF enforces this limit because StickyNote content is stored as base64 inside the annotation XML and unbounded notes would bloat the annotation store.

Solutions

  1. Reduce the StickyNote content (remove pasted bulk text/formatting) before the note is committed.
  2. Move large content out of the sticky note (store in a file/document) and keep only a short reference or summary in the note.
  3. Pre-check content size: serialize the RichTextRange yourself and warn the user before Save is invoked.
  4. Catch InvalidOperationException around save/commit (e.g. in response to losing focus or navigation) and surface a size warning instead of failing silently.

Example fix

// before
rtbRange.Save(buffer, DataFormats.Xaml); // may exceed MaxBufferSize
// after
rtbRange.Save(buffer, DataFormats.Xaml);
if (buffer.Length.CompareTo(MaxBufferSize) > 0)
{
    buffer.SetLength(0);
    rtbRange.Save(buffer, DataFormats.Text); // or trim content / warn user
}
Defensive patterns

Strategy: try-catch

Validate before calling

var buf = new MemoryStream(); rtbRange.Save(buf, DataFormats.Xaml);
bool withinLimit = buf.Length <= MaxBufferSize;

Try / catch

try { SaveNoteContent(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("size")) { MessageBox.Show("Sticky note content too large; reduce it."); }

Prevention

When it happens

Trigger: Saving a text StickyNote whose content, when saved as XAML from the inner RichTextBox, is larger than MaxBufferSize (~several KB), e.g. pasting large text or many formatted runs into a sticky note.

Common situations: Pasting documents or large code snippets into a sticky note, notes with heavy inline formatting that inflate XAML size, annotation stores migrated from apps without the limit.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/StickyNote/StickyNoteContentControl.cs:211

            /// <summary>
            /// Save the RichTextBox data to an Xml node
            /// </summary>
            /// <param name="node"></param>
            public override void Save(XmlNode node)
            {
                // make constant
                Debug.Assert(node != null && !IsEmpty);
                RichTextBox richTextBox = (RichTextBox)InnerControl;

                TextRange rtbRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                if (!rtbRange.IsEmpty)
                {
                    using (MemoryStream buffer = new MemoryStream())
                    {
                        rtbRange.Save(buffer, DataFormats.Xaml);

                        if (buffer.Length.CompareTo(MaxBufferSize) > 0)
                            throw new InvalidOperationException(SR.MaximumNoteSizeExceeded);

                        // Using GetBuffer avoids making a copy of the buffer which isn't necessary
                        // Safe cast because the array's length can never be greater than Int.MaxValue
                        node.InnerText = Convert.ToBase64String(buffer.GetBuffer(), 0, (int)buffer.Length);
                    }
                }
            }


            /// <summary>
            /// Load the RichTextBox data from an Xml node
            /// </summary>
            /// <param name="node"></param>
            public override void Load(XmlNode node)
            {
                Debug.Assert(node != null);
                RichTextBox richTextBox = (RichTextBox)InnerControl;

View on GitHub (pinned to 81131a70a4)