C#将richtextbox中的文字和图片转化成字节保存到数据库中,并从数据库中读取文字和图片,再显示出来
原创
©著作权归作者所有:来自51CTO博客作者mb630ec035bcfe8的原创作品,请联系作者获取转载授权,否则将追究法律责任
这里主要是将richtextbox中的内容转换成字节数组,主要是将字节流打包的过程:方法可用,已测试
命名空间
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Xml;using System.Text;
1,转换为字节
private byte[] GetRichTextBox(RichTextBox box)
{
MemoryStream m=new MemoryStream();
TextRange documentRange=new TextRange(box.Document.ContentStart,box.Document.ContentEnd);
documentRange.Save(m,DataFormats.XamlPackage);
return m.ToArray();
}
2,字节转换为RichTextBox数据
Private void showRichTextBox(RichTextBox rt,byte[] bytes )
{
MemoryStream m=new MemoryStream (bytes);
TextRange documentRange=new TextRange(box.Document.ContentStart,box.Document.ContentEnd);
documentRange.Load(m,DataFormats.XamlPackage);
}