【发布时间】:2010-12-21 15:55:08
【问题描述】:
我有一个富文本框,我计划将其保存到数据库中,可以将其加载回同一个富文本框。我已经让它工作了,这样我就可以将流文档保存为 DataFormats.XamlPackage,它可以保存图像,但问题是文本不可搜索。使用 DataFormats.Xaml,我当然有文本,但没有图像。图像将由最终用户粘贴,而不是应用程序中包含的图像。
我尝试使用 XamlWriter 将文本转换为 XML,然后分别从文档中获取图像并将它们作为二进制文件插入 XML,但我似乎找不到将图像转换为二进制文件的方法。 .
有没有人知道如何将图像转换为二进制,与文本分开?
提前致谢!
GetImageByteArray() 是问题所在。
代码:
private void SaveXML()
{
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
FlowDocument flowDocument = richTextBox.Document;
using (StringWriter stringwriter = new StringWriter())
{
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stringwriter))
{
XamlWriter.Save(flowDocument, writer );
}
testRTF t = new testRTF();
t.RtfText = new byte[0];
t.RtfXML = GetImagesXML(flowDocument);
t.RtfFullText = stringwriter.ToString();
//save t to database
}
richTextBox.Document.Blocks.Clear();
}
private string GetImagesXML(FlowDocument flowDocument)
{
using (StringWriter stringwriter = new StringWriter())
{
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stringwriter))
{
Type inlineType;
InlineUIContainer uic;
System.Windows.Controls.Image replacementImage;
byte[] bytes;
System.Text.ASCIIEncoding enc;
//loop through replacing images in the flowdoc with the byte versions
foreach (Block b in flowDocument.Blocks)
{
foreach (Inline i in ((Paragraph)b).Inlines)
{
inlineType = i.GetType();
if (inlineType == typeof(Run))
{
//The inline is TEXT!!!
}
else if (inlineType == typeof(InlineUIContainer))
{
//The inline has an object, likely an IMAGE!!!
uic = ((InlineUIContainer)i);
//if it is an image
if (uic.Child.GetType() == typeof(System.Windows.Controls.Image))
{
//grab the image
replacementImage = (System.Windows.Controls.Image)uic.Child;
//get its byte array
bytes = GetImageByteArray((BitmapImage)replacementImage.Source);
//write the element
writer.WriteStartElement("Image");
//put the bytes into the tag
enc = new System.Text.ASCIIEncoding();
writer.WriteString(enc.GetString(bytes));
//close the element
writer.WriteEndElement();
}
}
}
}
}
return stringwriter.ToString();
}
}
//This function is where the problem is, i need a way to get the byte array
private byte[] GetImageByteArray(BitmapImage bi)
{
byte[] result = new byte[0];
using (MemoryStream ms = new MemoryStream())
{
XamlWriter.Save(bi, ms);
//result = new byte[ms.Length];
result = ms.ToArray();
}
return result;
}
更新
我想我可能终于找到了解决方案,我将在下面发布。它使用 BmpBitmapEncoder 和 BmpBitmapDecoder。这使我可以从位图图像中获取二进制文件,将其存储到数据库中,然后将其重新加载并直接显示回 FlowDocument。初步测试证明是成功的。出于测试目的,我绕过了我的数据库步骤,基本上通过创建二进制文件来复制图像,然后将二进制文件转换为新图像并将其添加到 FlowDocument 中。唯一的问题是,当我尝试使用修改后的 FlowDocument 并使用 XamlWriter.Save 函数时,它会在新创建的图像上出错,并显示“无法序列化非公共类型 'System.Windows.Media.Imaging.BitmapFrameDecode”。这将需要进一步调查。不过我得先别管它了。
private void SaveXML()
{
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
FlowDocument flowDocument = richTextBox.Document;
string s = GetImagesXML(flowDocument);//temp
LoadImagesIntoXML(s);
using (StringWriter stringwriter = new StringWriter())
{
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stringwriter))
{
XamlWriter.Save(flowDocument, writer );//Throws error here
}
}
}
private string GetImagesXML(FlowDocument flowDocument)
{
string s= "";
using (StringWriter stringwriter = new StringWriter())
{
Type inlineType;
InlineUIContainer uic;
System.Windows.Controls.Image replacementImage;
byte[] bytes;
BitmapImage bi;
//loop through replacing images in the flowdoc with the byte versions
foreach (Block b in flowDocument.Blocks)
{
foreach (Inline i in ((Paragraph)b).Inlines)
{
inlineType = i.GetType();
if (inlineType == typeof(Run))
{
//The inline is TEXT!!!
}
else if (inlineType == typeof(InlineUIContainer))
{
//The inline has an object, likely an IMAGE!!!
uic = ((InlineUIContainer)i);
//if it is an image
if (uic.Child.GetType() == typeof(System.Windows.Controls.Image))
{
//grab the image
replacementImage = (System.Windows.Controls.Image)uic.Child;
bi = (BitmapImage)replacementImage.Source;
//get its byte array
bytes = GetImageByteArray(bi);
s = Convert.ToBase64String(bytes);//temp
}
}
}
}
return s;
}
}
private byte[] GetImageByteArray(BitmapImage src)
{
MemoryStream stream = new MemoryStream();
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)src));
encoder.Save(stream);
stream.Flush();
return stream.ToArray();
}
private void LoadImagesIntoXML(string xml)
{
byte[] imageArr = Convert.FromBase64String(xml);
System.Windows.Controls.Image img = new System.Windows.Controls.Image()
MemoryStream stream = new MemoryStream(imageArr);
BmpBitmapDecoder decoder = new BmpBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
img.Source = decoder.Frames[0];
img.Stretch = Stretch.None;
Paragraph p = new Paragraph();
p.Inlines.Add(img);
richTextBox.Document.Blocks.Add(p);
}
【问题讨论】:
-
可能希望将这篇文章的标题更改为更合适的名称...看来您真的在问如何获取 Image 对象的原始字节并将其存储在 Xml 文档中...
-
谢谢,听起来不错,我试图尽可能具体,以防有人对如何实现完整保存文本和二进制图像的相同效果有其他想法,但我没有认为这很可能
标签: c# wpf serialization image richtextbox