To get the formatted text that will be saved in the db:
string rtfText; //string to save to db
TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
tr.Save(ms, DataFormats.Rtf);
rtfText = Encoding.ASCII.GetString(ms.ToArray());
}
To restore the formatted text retrieved from the db:
string rtfText= ... //string from db
byte[] byteArray = Encoding.ASCII.GetBytes(rtfText);
using (MemoryStream ms = new MemoryStream(byteArray))
{
TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
tr.Load(ms, DataFormats.Rtf);
}
You can also use XAML format instead, using DataFormats.XAML on load an save.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…