问题描述
我需要将我的richText text + image保存到数据库中.这是我的代码:
私有 无效 cmdSave_Click( 对象 发送者, RoutedEventArgs e) span>
SaveFileDialog saveFile = 新 SaveFileDialog (span style ="font-size:xx-small">(); 文件(* .xaml)| * .xaml" ;
如果 (saveFile.ShowDialog()== 真 ) //在整个文档中创建一个TextRange.
{{p
TextRange documentTextRange = 新 TextRange ( richTextBox1.Document.ContentStart,richTextBox1.Document.ContentEnd);
////如果此文件存在,则会被覆盖.
使用 ( FileStream fs = 文件 .Create(saveFile.FileName))
documentTextRange.Save(fs, Dat aFormats .XamlPackage);
}
}
字节 []字节;
使用 ( FileStream fs = 新 FileStream (saveFile.FileName, FileMode .Open) )
.{
new 字节 [fs.Length];
fs.Read(bytes,0,( int )fs.Length);
数据=字节;
}
数据";是一个局部变量,用于实验数据库负载.
私有 无效 cmdOpen_Click( 对象 发送者, RoutedEventArgs e)
{
OpenFileDialog openFile = 新 OpenFileDialog ();
使用 ( MemoryStream ms = 新 MemoryStream (数据)){
FlowDocument fl = 新 FlowDocument ();
TextRange tr = 新 TextRange (fl.ContentStart ,fl.ContentEnd); DataFormats .XamlPackage);
richTextBox1.Document = fl;
tr.Load(ms,
ms.Close();
}
}
当使用" tr.Load(ms, 数据格式 .XamlPackage); "指令,我收到此错误:"数据格式为'XamlPackage'的无法识别的结构.参数名称:stream "
我不知道收到此错误的原因,我的所有代码对我来说似乎很好.您有任何想法或解决方案吗?
谢谢.
Hi,
I need to save my richText text+image to the database. Here is my code :
private void cmdSave_Click(object sender, RoutedEventArgs e) {
SaveFileDialog saveFile = new SaveFileDialog(); "Files (*.xaml)|*.xaml";
if (saveFile.ShowDialog() == true) //Create a TextRange around the entire document.
{
TextRange documentTextRange = new TextRange( richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
//If this file exists, it's overwritten.
using (FileStream fs = File.Create(saveFile.FileName))
{
documentTextRange.Save(fs, DataFormats.XamlPackage);
}
}
byte[] bytes;
using (FileStream fs = new FileStream(saveFile.FileName, FileMode.Open))
{
bytes = new byte[fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
data = bytes;
}
"data" is a local variable used in order to experiment the load from the database.
private void cmdOpen_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
using (MemoryStream ms = new MemoryStream(data)) {
FlowDocument fl = new FlowDocument();
TextRange tr = new TextRange(fl.ContentStart, fl.ContentEnd); DataFormats.XamlPackage);
richTextBox1.Document = fl;
tr.Load(ms,
ms.Close();
}
}
When loading my content using "tr.Load(ms, DataFormats.XamlPackage);" instruction, I get this error : "Unrecognized structure in data format 'XamlPackage'. Parameter name: stream"
I don't know the reason of getting this error all my code seems to me fine. Do you have any idea or solution?
Thank you.
这篇关于将richText内容+图像保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!