将richText内容+图像保存到数据库 [英] Save the richText content+image to the database
问题描述
我需要将我的richText text + image保存到数据库中.这是我的代码:
无效 cmdSave_Click( 对象 发送者, saveFile = 新 SaveFileDialog (span style ="font-size:xx-small">(); ;
(saveFile.ShowDialog()== 真 ) //在整个文档中创建一个TextRange.
{{p
documentTextRange = 新 TextRange ( richTextBox1.Document.ContentStart,richTextBox1.Document.ContentEnd);
( FileStream 文件 .Create(saveFile.FileName)) documentTextRange.Save(fs, Dat aFormats .XamlPackage);
}
}
[]字节;
使用 FileStream fs = FileStream new [fs.Length];
fs.Read(bytes,0,( int )fs.Length);
数据=字节;
}
数据";是一个局部变量,用于实验数据库负载.
私有 cmdOpen_Click( 对象 RoutedEventArgs e)
{
OpenFileDialog openFile = OpenFileDialog
使用 ms = (数据)){ FlowDocument fl = FlowDocument
TextRange tr = TextRange DataFormats .XamlPackage);
richTextBox1.Document = fl;
tr.Load(ms,
ms.Close();
}
}
当使用" tr.Load(ms, 数据格式 .XamlPackage); "指令,我收到此错误:"数据格式为'XamlPackage'的无法识别的结构.参数名称:stream "
我不知道收到此错误的原因,我的所有代码对我来说似乎很好.您有任何想法或解决方案吗?
谢谢.
一行代码中的错误"tr.Load(ms,"应为"tr.Load(ms,DataFormats.XamlPackage);"
检查要加载的数据是否是xaml文件,xamlpackage类型是二进制文件,而不是像xaml这样的文本文件.
我在本文
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.
解决方案There is an error in one line of code "tr.Load(ms," should be "tr.Load(ms, DataFormats.XamlPackage);"
Check if the data that you are loading is a xaml file, the xamlpackage type is a binary file not a text file like the xaml.
I post an answer to your question in this post http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e4b322c2-2553-4802-bd0d-fe7ef7e90b37
You colud save your data to an 'image' or ntext column.
If you want to save in an 'image' column you can save the byte array, but for the ntext column you must use the Convert class.
Hope this help.
这篇关于将richText内容+图像保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!