Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If all you had was basic types all you would need to do is put a <a href="http://msdn.microsoft.com/en-us/library/system.serializableattribute%28v=vs.110%29.aspx" rel="nofollow"><code>[Serializable]</code></a> attribute on your class and you could use a <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.110%29.aspx" rel="nofollow"><code>XmlSeralizer</code></a> to write it out to a file.</p> <pre><code>[Serializable] public class cEspecie { //...Snip... } </code></pre> <p>However for that to work all of the elements inside the class must also be marked <code>[Serializable]</code>, unfortunately <code>BitmapImage</code> is not.</p> <p>To work around this we mark the unseralizeable object to be skipped, we then provide a new property that can be seralized that can represent the image</p> <pre><code>[Serializable] public class cEspecie { //...Snip... [XmlIgnore] //Skip this object public BitmapImage Imagen { get { return imagen; } set { imagen = value; } } [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] //This creates a secret hidden property that does not show up in intellisense [XmlElement("Imagen")] //It uses the name of the real property for it's name public BitmapImageSearalizer ImagenSerialized { get { return new BitmapImageSearalizer(imagen); } set { imagen = value.GetSearalizedImage(); } } } [Serializable] internal BitmapImageSearalizer { public BitmapImageSearalizer(BitmapImage sourceImage) { //...Snip... } public BitmapImage GetSearalizedImage() { //...Snip... } //...Snip... } </code></pre> <p>I leave it to you to write <code>BitmapImageSearalizer</code> I have not worked with the class enough to write it. </p> <p>To show you a more complete known working example of how to do it, here is a snippet from one of my projects where I did this exact trick with just a plain <a href="http://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx" rel="nofollow"><code>Bitmap</code></a> object</p> <pre><code> /// &lt;summary&gt; /// 1 bit per pixel bitmap to use as the image source. /// &lt;/summary&gt; [XmlIgnore] public Bitmap BitmapImage { get { return _Bitmap; } set { if (value.PixelFormat != System.Drawing.Imaging.PixelFormat.Format1bppIndexed) throw new FormatException("Bitmap Must be in PixelFormat.Format1bppIndexed"); _Bitmap = value; } } [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] [XmlElement("Bitmap")] public byte[] BitmapSerialized { get { // serialize if (BitmapImage == null) return null; using (MemoryStream ms = new MemoryStream()) { BitmapImage.Save(ms, ImageFormat.Bmp); return ms.ToArray(); } } set { // deserialize if (value == null) { BitmapImage = null; } else { using (MemoryStream ms = new MemoryStream(value)) { BitmapImage = new Bitmap(ms); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload