XamlWriter image serialization isssue (InvalidOperationException: Property 'UriSource' or property 'StreamSource' must be set.)

Naveen Kumar R 1 Reputation point


i am using a ink canvas and added a image to it and then serialize using XamlWriter.

Image img = new Image();
BitmapImage img = NImageHelper.GetBitmapImageFromString(ImageData, new Size(src.Width, src.Height));
img.Source = img;

string Xml = XamlWriter.Save(myCanvas);

this code works fine, Here is the xml generated

<Image Height="100" Width ="150">
<BitmapImage CacheOption="None" CreateOptions="IgnoreImageCache" BaseUri="{x:Null}"/>

in this xml bitmapimage's StreamSource property is not serialized

When i try to parse it back it is throwing exception

System.Windows.Markup.XamlParseException: ''Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception.'
InvalidOperationException: Property 'UriSource' or property 'StreamSource' must be set.

It works fine if the bitmapimage's UriSource property is set and i am able to parse it back.

Please help me , how to serialize StreamSource property?

Thanks in advance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,709 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points

    Welcome to our Microsoft Q&A platform!

    You can set UriSource = "" for BitMapImage or not set Image.Source in the XAML, then set the image source in the cs.You can refer to the examples in the document for showing the picture.

    0 comments No comments