title: Storing and displaying rich text efficiently
body: <p>I need to store significant amounts of rich text in a SQL database, retrieve it and display it.</p>
  
  <p>One font throughout is OK but I need different font sizes/bold/colors.</p>
  
  <p>For now I am using a RichTextBox (WPF) to display it, and XamlWriter.Save/XamlReader.Parse to serialize it to strings to store in the DB. It works well but the RichTextBox is so HUGELY SLOW at displaying the text that it's basically unusable.</p>
  
  <p>Is there a quick way to do this with acceptable performance?</p>
  
  <p>I'm considering doing it with GlyphRun objects, drawing each character as a bitmap and computing all the alignment requirements to fit the destination image etc... But reinventing the wheel on simple colored/sizable text seems really strange in 2011.</p>
  
  <p><strong>EDIT</strong>:
  Thanks for the answers, didn't see them until now, sorry.</p>
  
  <p>Text is entered by the user from <code>RichTextBox</code>es as well, basically I just save the resulting <code>string</code> <code>XamlWriter.Save(richTextBox.Document)</code> in the database. Other fields (double/int etc) are also entered by the user from <code>TextBox</code>es.</p>
  
  <p>As the user queries the database, pages of read-only rich text with colors and formatting is generated from scratch using the fields in the database, including the saved rich text fields above: these are converted from <code>FlowDocument</code>s to <code>Span</code>s and some replacement is done on them (<code>InlineUIContainer</code>s which host a class derived from <code>UIElement</code> which references a database entry, inlined in the text, like "see [thisbook]" where [thisbook] references some database entry's ID). MSDN says all that is far too much text for a <code>TextBlock</code>.</p>
  
  <p>That text rendering is the really slow part but there is no way around it, I need that formatting and it's just how the WPF <code>RichTextBox</code>es are: even when entering a little simple text in the <code>RichTextBox</code>es, there is a delay between typing and the character appearing on the screen...</p>
  
  <p>For now I still use <code>RichTextBox</code>es but I keep lots of rendered layouts in memory (the <code>Paragraph</code>/<code>Section</code>/<code>Span</code> objects) and I am careful to rerender only the least amount of formatted text possible when changes/queries are made or different views of the database data are requested by the user.</p>
  
  <p>It's still not fast but it's OK, changing the whole structure (AvalonEdit or <code>FormattedText</code> or <code>GlyphRun</code>) doesn't seem worth it right now, too much work, the whole serialization API with <code>XamlWriter.Save</code> and <code>XamlReader.Parse</code> simplifies much (for <code>FormattedText</code> and <code>GlyphRun</code>, I'd have to come up with a file format myself to save the formatted text to the database).</p>
  
  <p>There is also the possibility of using the OpenXML SDK to create Microsoft Word .docx documents but google says rendering performance isn't great either, and I don't know if embedding an <code>UIElement</code> in the text within an <code>InlineUIContainer</code> and serializing that to be saved in the database would be possible (same problem with AvalonEdit).</p>