0
  • Is it possible to load XAML file with images from stream into WPF application?
  • And how to store this images in the files?

I've tried <x:code> part, but XamlReader.Load method can't process it.

XAML Code:

    <Page x:Class="System.Windows.Controls.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded"
>
<Canvas Width="500" Height="1000">
</Canvas>
<x:Code>
<![CDATA[
void onLoaded(object sender, RoutedEventArgs e)
{
Canvas cvs= this.Content as Canvas;
Image img = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
string image64 = "...base64image...";
byte[] bd = Convert.FromBase64String(image64);
bi.StreamSource = new System.IO.MemoryStream(bd);
bi.EndInit();
img.Source = bi;
Thickness margin = img.Margin;
margin.Left = 10;
margin.Top = 20; 
img.Margin = margin;
cvs.Children.Add(img);
}
]]>
</x:Code>
</Page>

C# code:

FileStream fs = new FileStream(@"C:\...\XAML\img_xaml.xaml", FileMode.Open);

            Window w = new Window();

            object xaml = XamlReader.Load(fs);
            w.Content = xaml;
            w.Show();

Exception -

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

0 Answers0