【问题标题】:C# WPF convert Image to object and backC# WPF 将图像转换为对象并返回
【发布时间】:2015-11-25 21:40:53
【问题描述】:

这是一个演变: C# image binary serialization

我的课很简单:

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

为了序列化它,我必须将 myImg 从图像转换为对象

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

但 ist.myImg 仍然是图像。

感谢任何帮助 帕特里克

【问题讨论】:

  • 您似乎误解了Convert.ChangeType() 方法的作用。您从哪里得知它会序列化您的数据?正如您在上一个问题中所解释的,您需要以适合您场景的格式明确保存图像数据。有很多方法可以做到这一点,所有这些都记录在 MSDN 上,并在 Stack Overflow 上的各种问答中进行了描述。

标签: c# image object


【解决方案1】:

是的,我错了。最后这么简单:

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

然后序列化为:

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;
    }
  }
}

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2015-10-11
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多