我想創建一個應用程序,我可以在RichTextBox中輸入文本和圖像,將其序列化,然後反序列化並將其加載回RichTextBox,以便我可以稍後改變它。當我從序列化的xml文件加載圖像時,所有內容都能正確顯示,但是當我嘗試通過按下退格鍵手動從RichTextBox中刪除圖像時,出現以下異常:無法序列化非公開類型的「System.Windows.Media .Imaging.BitmapFrameDecode」。InvalidOperationException當我嘗試從WPF中的RichTextBox手動刪除圖像
下面是我如何從RichTextBox中提取和存儲數據。它檢查所有塊,如果發現一個圖像,然後它只是在列表中保存的文本佔位符串存在,這樣,當它返回它就會知道把圖像回到該位置:
public void GetFindingsData(FlowDocument flowDoc, List<string> text, List<byte[]> bytes)
{
foreach (Block block in flowDoc.Blocks)
{
if (block.GetType() == typeof(Paragraph))
{
foreach (Run run in ((Paragraph)block).Inlines)
{
text.Add(run.Text);
}
}
else if (block.GetType() == typeof(BlockUIContainer) && ((BlockUIContainer)block).Child.GetType() == typeof(Image))
{
Image img = (Image)((BlockUIContainer)block).Child;
bytes.Add(Storage.ImageToByteArray(img));
text.Add("imageplaceholder_" + (bytes.Count - 1).ToString());
}
}
}
這裏就是我如何把這些數據返回到一個FlowDocument的顯示在RichTextBox:
public FlowDocument createFlowDocument(List<string> runs, List<byte[]> bytes)
{
FlowDocument flowDoc = new FlowDocument();
int counter = 0;
foreach (string run in runs)
{
if (run == "imageplaceholder_" + counter.ToString())
{
flowDoc.Blocks.Add(new BlockUIContainer(Storage.ByteArrayToImage(bytes[counter])));
counter++;
}
else
{
Paragraph par = new Paragraph();
par.Inlines.Add(run);
flowDoc.Blocks.Add(par);
}
}
return flowDoc;
}
如果你需要它,這裏就是我從RichTextBox的序列化數據。我的所有其他數據序列化爲XML,但是,這並不對圖像的工作,所以我首先它序列化到一個字節數組:
public static byte[] ImageToByteArray(Image image)
{
byte[] imageBuffer = null;
if (image != null)
{
using (var stream = new MemoryStream())
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image.Source as BitmapSource));
encoder.Save(stream);
imageBuffer = stream.ToArray();
}
}
return imageBuffer;
}
這是我序列化和從XML文件反序列化的一切/(雖然我不認爲這個問題是在這裏):
public static void SaveData(StoredData data)
{
XmlSerializer serializer = new XmlSerializer(typeof(StoredData));
using (FileStream stream = new FileStream(readerString, FileMode.Create, FileAccess.Write, FileShare.None))
{
serializer.Serialize(stream, data);
}
}
public static StoredData LoadData()
{
try
{
StoredData storedData = new StoredData();
using (FileStream stream = new FileStream(readerString, FileMode.Open))
{
XmlSerializer deserializer = new XmlSerializer(typeof(StoredData));
storedData = (StoredData)deserializer.Deserialize(stream);
}
return storedData;
}
catch
{
StoredData newData = new StoredData();
XmlSerializer serializer = new XmlSerializer(typeof (StoredData));
using (FileStream stream = new FileStream(readerString, FileMode.Create, FileAccess.Write, FileShare.None))
{
serializer.Serialize(stream, newData);
}
return newData;
}
}
這裏就是我得到的圖像回從字節數組:
public static Image ByteArrayToImage(Byte[] imageBytes)
{
using (MemoryStream stream = new MemoryStream(imageBytes))
{
BitmapDecoder decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapFrame frame = decoder.Frames.First();
frame.Freeze();
Image newImage = new Image();
newImage.Source = frame;
return newImage;
}
}
任何幫助將不勝感激。
您是否看到[link](http://stackoverflow.com/questions/1820098/c-sharp-wpf-convert-bitmapimage-pasted-in-richtextbox-to-binary)?這似乎與你的問題類似。 –
我做過了,如果我錯了,請糾正我的錯誤,但似乎他們只是暫時做了同樣的問題,然後才決定採用不同的方法來解決問題。如果我沒有足夠強調我的問題,我沒有任何問題從RichTextBox保存或加載到RichTextBox。只有當我嘗試刪除重新載入的圖片時,我遇到了這個錯誤。即使刪除重新加載的文本也能正常工 – mrmry
是的,我知道了。那麼,當你得到這個異常?在哪一行?你在RichTextBox的文字改變事件中做什麼? –