0

This is an evolution of: C# image binary serialization

I have very simple class:

public class TheClass2
{
   public object myImg;
   public int myInt;
}

In order to serialize it I have to cast myImg from image to object

var ist = new TheClass2();
Image i = new Image();
ist.myImg= Convert.ChangeType(i, typeof(object));<-----this is not working

but ist.myImg is still an image.

Thanx for any help Patrick

Community
  • 1
  • 1
Patrick
  • 3,073
  • 2
  • 22
  • 60
  • You seem to misunderstand what the `Convert.ChangeType()` method does. Where did you get the idea that it would _serialize_ your data? As explained in your previous question, you will need to save your image data explicitly in a format appropriate to your scenario. There are lots of ways to do this, all documented on MSDN and described in various Q&As on Stack Overflow. – Peter Duniho Nov 23 '15 at 06:52

1 Answers1

1

Yes I was wrong. So easy in the end:

public class MyBitmapImage
{
        public string strBitmapImage;
        public bool IsImageEmbedded;
}

and then serialize as:

public static bool FileSerializer<T>(string filePath, T objectToWrite, out string strError, bool append = false)
{
  using (Stream fileStream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
  {
    strError = string.Empty;
    try
    {
      var binaryFormatter = new BinaryFormatter();
      binaryFormatter.Serialize(fileStream, objectToWrite);
      return true;
    }
    catch (Exception exc)
    {
      strError = "Binary FileSerializer exception:" + exc;
      return false;
    }
  }
}
Patrick
  • 3,073
  • 2
  • 22
  • 60