/// <summary> /// Writes embeded object tag. /// </summary> /// <param name="embeddedObject"> /// </param> /// <param name="xmlWriter"> /// XmlWriter to output element opening tag. /// </param> /// <param name="wpfPayload"> /// </param> private static void WriteEmbeddedObject(object embeddedObject, XmlWriter xmlWriter, WpfPayload wpfPayload) { if (wpfPayload != null && embeddedObject is Image) { // Writing in WPF mode: need to create an image with a Source referring into a package Image image = (Image)embeddedObject; if (image.Source != null && !string.IsNullOrEmpty(image.Source.ToString())) { // Add the image to the Image collection in the package // and define the reference to image into the package string imageSource = wpfPayload.AddImage(image); if (imageSource != null) { Type elementTypeStandardized = typeof(Image); // Write opening tag for the element xmlWriter.WriteStartElement(elementTypeStandardized.Name); // Write all properties except for Source DependencyProperty[] imageProperties = TextSchema.ImageProperties; DependencyObject complexProperties = new DependencyObject(); for (int i = 0; i < imageProperties.Length; i++) { DependencyProperty property = imageProperties[i]; if (property != Image.SourceProperty) { object value = image.GetValue(property); // Write the property as attribute string or add it to a list of complex properties. WriteNoninheritableProperty(xmlWriter, property, value, elementTypeStandardized, /*onlyAffected:*/true, complexProperties, image.ReadLocalValue(property)); } } // Write Source property - as a local reference into the package container // Write Source property as the complex property to specify the BitmapImage // cache option as "OnLoad" instead of the default "OnDeman". Otherwise, // we couldn't load the image by disposing WpfPayload package. xmlWriter.WriteStartElement(typeof(Image).Name + "." + Image.SourceProperty.Name); xmlWriter.WriteStartElement(typeof(System.Windows.Media.Imaging.BitmapImage).Name); xmlWriter.WriteAttributeString(System.Windows.Media.Imaging.BitmapImage.UriSourceProperty.Name, imageSource); xmlWriter.WriteAttributeString(System.Windows.Media.Imaging.BitmapImage.CacheOptionProperty.Name, "OnLoad"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); // Write remaining complex properties WriteComplexProperties(xmlWriter, complexProperties, elementTypeStandardized); // Close the element xmlWriter.WriteEndElement(); } } } else { // In non-package mode we ignore all UIElements. // Output a space replacing this embedded element. // Note that in this mode (DataFormats.Xaml) InlineUIContainer was // replaced by Run and BlockUIContainer - by Paragraph, // so the space output here will be significant. xmlWriter.WriteString(" "); } }