none
XamlWriter/XamlReader image serialization isssue[XamlParseException ]

    Question

  • Hi,

    I am using and InkCanvas and added a Image on it and then try to serialize it through XamlWriter:

    Image img = new Image();
    img.Source = imgSource;
    _inkcanvas.Children.Add(img)

    string strXML = XamlWriter.Save(_inkCanvas);

    This code work fine. Here is the XML it generates for Image:

    <av:Image av:InkCanvas.Top="53" av:InkCanvas.Left="221">
        <av:Image.Source>
          <swi:InteropBitmap />
        </av:Image.Source>    
      </av:Image>


    But, when I tried to parse it back to xaml like:

    _inkCanvas = (InkCanvas)XamlReader.Load(new XmlTextReader(new StringReader(value))); // value = XML generated

    it is throwing XamlParseException :

    "Cannot create object of type 'System.Windows.Interop.InteropBitmap'. CreateInstance failed, which can be caused by not having a public default constructor for 'System.Windows.Interop.InteropBitmap'.  Error at object 'System.Windows.Controls.Image', Line 1 Position 5489."

    It woks fine with other controls like TextBoxes, Strokes etc.
    Anybody has any idea how to get it working with Images also?  Please let me know if you need further explanation.

     

    Wednesday, February 24, 2010 10:24 AM

Answers

  • Hi,

    Serializing then deserializing an InteropBitmap isn’t supported:  it does not have a public constructor.  This blog has a good explanation: http://blogs.msdn.com/mikehillberg/archive/2006/09/16/XamlWriter.aspx.

     However, using a simple BitmapImage as the image source works, if that fits your scenario, e.g.,

    BitmapImage imgSource = null;

    imgSource = new BitmapImage(new Uri("pack://application:,,,/XamlWriterSave;component/M.gif", UriKind.RelativeOrAbsolute));

     

    Image img = new Image();

    img.Source = imgSource;

    InkCanvas _inkCanvas1 = new InkCanvas();

    _inkCanvas1.Children.Add(img);

     

    string strXML = XamlWriter.Save(_inkCanvas1);

    InkCanvas _inkCanvas2 = (InkCanvas)XamlReader.Load(new XmlTextReader(new StringReader(strXML)));

    • Marked as answer by coder_s Thursday, February 25, 2010 4:48 PM
    Wednesday, February 24, 2010 11:29 PM

All replies

  • Hi,

    Serializing then deserializing an InteropBitmap isn’t supported:  it does not have a public constructor.  This blog has a good explanation: http://blogs.msdn.com/mikehillberg/archive/2006/09/16/XamlWriter.aspx.

     However, using a simple BitmapImage as the image source works, if that fits your scenario, e.g.,

    BitmapImage imgSource = null;

    imgSource = new BitmapImage(new Uri("pack://application:,,,/XamlWriterSave;component/M.gif", UriKind.RelativeOrAbsolute));

     

    Image img = new Image();

    img.Source = imgSource;

    InkCanvas _inkCanvas1 = new InkCanvas();

    _inkCanvas1.Children.Add(img);

     

    string strXML = XamlWriter.Save(_inkCanvas1);

    InkCanvas _inkCanvas2 = (InkCanvas)XamlReader.Load(new XmlTextReader(new StringReader(strXML)));

    • Marked as answer by coder_s Thursday, February 25, 2010 4:48 PM
    Wednesday, February 24, 2010 11:29 PM
  • Hi Bob,

    Thanks for your reply!

    just one thing, can you please describe below line in bit more detail:

    "new Uri("pack://application:,,,/XamlWriterSave;component/M.gif", UriKind.RelativeOrAbsolute)"

    I am not getting how you are creating the path here.

    Thanks,
    Thursday, February 25, 2010 4:52 PM
  • That format is used for resource files that are compiled into the local assembly.  The complete explanation is here:  http://msdn.microsoft.com/en-us/library/aa970069.aspx.  The article provides some examples of specifying the path for the resource file:

    The following example shows the pack URI for a XAML resource file that is located in the root of the referenced assembly's project folder.

    pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

    The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.

    pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml

     

    Hope this helps.

    Thursday, February 25, 2010 11:02 PM