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

1
  • 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. Commented Nov 23, 2015 at 6:52

1 Answer 1

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;
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.