title: How to serialize a Class contains BitmapImage?
body: <p>I have a DeepCopy method, which serializes the object passed in parameter and returns back the deserialized object to make deep copy.</p>
  
  <p>My method is:</p>
  
  <pre><code>public static class GenericCopier&lt;T&gt;
  {     
             public static T DeepCopy(object objectToCopy)
              {
                  using (MemoryStream memoryStream = new MemoryStream())
                  {
                      BinaryFormatter binaryFormatter = new BinaryFormatter();
                      binaryFormatter.Serialize(memoryStream, objectToCopy);
                      memoryStream.Seek(0, SeekOrigin.Begin);
                      return (T)binaryFormatter.Deserialize(memoryStream);
                  }
              }
  }
  </code></pre>
  
  <p>It works well, if the object passed to the parameter doesn't contain any BitmapImage Field and Properties.</p>
  
  <pre><code>public class MyClass
  {
    public string TestString {get; set;}
    public BitmapImage TestImage { get; set;}
  }
  </code></pre>
  
  <p>If I make DeepCopy of MyClass, </p>
  
  <pre><code>MyClass orginal = new MyClass(){ TestString = "Test"};
  MyClass copy = GenericCopier&lt;MyClass&gt;.DeepCopy(orginal);
  </code></pre>
  
  <p>it throws exception </p>
  
  <p><em>Type 'System.Windows.Media.Imaging.BitmapImage' in Assembly  is not marked as serializable</em></p>
  
  <p>I found a method to serialize BitmapImage <a href="https://stackoverflow.com/questions/4308777/wpf-bitmapimage-serialization-deserialization">here</a></p>
  
  <p>But, How can i mix the both type of serialization (BinaryFormatter &amp; PngBitmapEncoder) to serialize MyClass?</p>