Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
[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?
Posted
Comments
lukeer 28-May-13 7:44am    
Usually, you don't serialize a PictureBox but instead the picture within. Are you sure you do want to serialize the GUI element PictureBox?
Rasool Ahmed 28-May-13 7:48am    
Yes, I'm sure because picturebox contains "SerialLabel" controls which is another GUI element specialized for a purpose. I want save all picturebox statuses and load it again later.

1 solution

Hi,

I can show you a tricky way

C#
[Serializable]
public class MyPictureBox:PictureBox
{
 // create your properties which you need to Serialize
// eg:
public int Width {get;set;}
}


Now Serialize class MyPictureBox

[update]
Try this[^]
 
Share this answer
 
v2
Comments
lukeer 28-May-13 8:53am    
OP has been that far already.
[Quote]
[Serializable]
public class sPictureBox : PictureBox {} //I tried this in order to get rather from the error but hopless :(
[/Quote]
Suvabrata Roy 28-May-13 9:05am    
please tell which properties you need( the list )
now
you can write implicit casting in both way for those properties
then you dont need any inheritance
Rasool Ahmed 28-May-13 10:02am    
All of them.
Suvabrata Roy 28-May-13 13:17pm    
but there are some datatypes which never been serialized, I dont think you need all properties or all properties visible to you.
Rasool Ahmed 28-May-13 14:55pm    
Just tell me what should I do to save picturebox and all its status in the file?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900