在c#中序列化,反序列化pictrurebox [英] Serialize, Deserialize pictrurebox in c#
问题描述
大家好,
我想将picturebox的内容和一些额外的数据保存到一个文件中。问题是,当我尝试将picturebox序列化到内存流时,在Assembly''System.Windows.Forms,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089中出现错误Type'System.Windows.Forms.PictureBox'' ''未标记为可序列化。
我的代码:
Hi guys,
I want to save content of picturebox and some additional data with it into one file. The problem is when I try to serialize picturebox to memorystream an error occured "Type ''System.Windows.Forms.PictureBox'' in Assembly ''System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'' is not marked as serializable."
My code:
[Serializable]
public class SaveSettings //this what I want to save to the file
{
public sPictureBox Receipt { get; set; } //This is the picturebox I told you for.
public Point ReceiptNoInPaper { get; set; }
public ReceiptsSerial.ReceiptSettings.ReceiptsNo ReceiptsNoMethod { get; set; }
public int ReceiptsTotalNumber { get; set; }
public string SerialNoFormat { get; set; }
public int XSpaces { get; set; }
public int YSpaces { get; set; }
public PageSettings PageSettings { get; set; }
public ReceiptsSerial.ReceiptSettings.language Lanaguage { get; set; }
public byte[] ConvertToBytes()
{
BinaryFormatter BF = new BinaryFormatter();
MemoryStream MS = new MemoryStream();
BF.Serialize(MS, this);
MS.Close();
return MS.ToArray();
}
public SaveSettings ConvertToObject(byte[] Data)
{
BinaryFormatter BF = new BinaryFormatter();
MemoryStream MS = new MemoryStream();
MS.Write(Data, 0, Data.Length);
MS.Seek(0, SeekOrigin.Begin);
SaveSettings obj = (SaveSettings)BF.Deserialize(MS);
MS.Close();
return obj;
}
}
[Serializable]
public class sPictureBox : PictureBox {} //I tried this in order to get rather from the error but hopless :(
那么,我该如何解决这个问题?
So, how can I solve the problem?
推荐答案
我可以告诉你一个棘手的方式
Hi,
I can show you a tricky way
[Serializable]
public class MyPictureBox:PictureBox
{
// create your properties which you need to Serialize
// eg:
public int Width {get;set;}
}
现在序列化类MyPictureBox
[更新]
尝试这个 [ ^ ]
这篇关于在c#中序列化,反序列化pictrurebox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!