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

Naveen Kumar R 1 Reputation point
2020-06-03T11:03:49.647+00:00

Hi,

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">
<Image.Source>
<BitmapImage CacheOption="None" CreateOptions="IgnoreImageCache" BaseUri="{x:Null}"/>
</Image.Source>
</Image>

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
    2020-06-04T06:58:53.383+00:00

    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