Problem when serializing and de-serializing RichTextBox in WPF

3

I have an experimental WPF probject like Microsoft Word. The main component is RichTextBox. Users can edit the content and save it to somewhere. The below methods are used in serializing and deserializing the content to/from a remote database.

public static byte[] FlowDocumentSerializingV2WithXamlWriter(FlowDocument flowDocument) {
    using(MemoryStream buffer = new MemoryStream()) {
        XamlWriter.Save(flowDocument, buffer);
        return buffer.ToArray();
    }
}

I serialize RichTextBox.Document using the above method. This method returns a byte[]. I save the byte[] result to a remote database. Later, we can read this byte[] data from the remote database and then load it into the RichTextBox using below method.

public FlowDocument FlowDocumentDeserializingV2WithXamlLoader(byte[] data) {
    MemoryStream ms = new MemoryStream(data);          
    return (FlowDocument)XamlReader.Load(ms);
}

If there is no image in the FlowDocument, the above methods work very well. The problem is that when any picture is pasted in the FlowDocument, FlowDocumentDeserializingV2WithXamlLoader(byte[] data) will throw an exception on the line return (FlowDocument)XamlReader.Load(ms). The exception is:

System.Windows.Markup.XamlParseException

HResult=0x80131501
Message='Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception.' Line number '1' and line position '123355'.
Source=PresentationFramework
StackTrace: at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)

This exception was originally thrown at this call stack:

System.Net.WebRequest.Create(System.Uri, bool)  
MS.Internal.WpfWebRequestHelper.CreateRequest(System.Uri)  
System.IO.Packaging.PackWebRequest.GetRequest(bool)  
System.IO.Packaging.PackWebRequest.GetResponse()  
MS.Internal.WpfWebRequestHelper.GetResponse(System.Net.WebRequest)
System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(System.Uri, System.IO.Stream, System.Windows.Media.Imaging.BitmapCacheOption, out System.Guid, out bool, out System.IO.Stream, out System.IO.UnmanagedMemoryStream, out Microsoft.Win32.SafeHandles.SafeFileHandle)
System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(System.Uri, System.Uri, System.IO.Stream, System.Windows.Media.Imaging.BitmapCreateOptions, System.Windows.Media.Imaging.BitmapCacheOption, System.Net.Cache.RequestCachePolicy, bool)
System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
System.Windows.Media.Imaging.BitmapImage.EndInit()
MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(System.Xaml.XamlType, object, bool)

Inner Exception 1:
NotSupportedException: The URI prefix is not recognized.

I don't know how to fix this problem. Can anybody help me? Any help is appreciating.

c#
wpf
serialization
asked on Stack Overflow Jul 30, 2020 by user2402181 • edited Jul 31, 2020 by Keith Stein

1 Answer

0

I have created a project to demonstrate this issue in Github: https://github.com/apchee/richtextbox

The images in readme.md is not showing, but it should be shown in local computer.

answered on Stack Overflow Aug 10, 2020 by user2402181

User contributions licensed under CC BY-SA 3.0