2015-11-23 22 views
0

這是一種演變: C# image binary serializationC#WPF轉換圖片爲對象和背部

我有非常簡單的類:

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仍然是圖像。

感謝名單的任何幫助 帕特里克

+0

你似乎誤解了'Convert.ChangeType()'方法做什麼。你從哪裏得到這樣的想法:它會對你的數據進行序列化?正如您在上一個問題中所解釋的那樣,您需要以適合於您的方案的格式顯式保存圖像數據。有很多方法可以做到這一點,所有這些都記錄在MSDN上,並在堆棧溢出的各種問答中進行描述。 –

回答

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