Click here to Skip to main content
14,775,260 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:
[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

[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[^]
   
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?
lukeer 29-May-13 2:01am
   
I don't believe you.
Have a look at MSDN[^]'s site on PictureBox. There you have listed all properties. And now think of Parent[^]. Do you need to serialize the Parent property?
That would ultimately result in serializing your application's complete state. Including all windows, current mouse position, screen size and so on.

Tell us what you're ultimately trying to achieve. Maybe we can work out what you really need at this point.
Rasool Ahmed 29-May-13 2:06am
   
So, you are telling me that if I want to save a GUI element I must make a class and list all properties of the control and save it??
Or save some of its important properties that I need to save its status??
Are you telling me that?
Suvabrata Roy 29-May-13 2:18am
   
He is try to tell you that you should be sure about what you want
Then we can help you out, as you can understand your requirement is quite difficult to understand.
Please tell use what you exactly want to store.
Rasool Ahmed 29-May-13 2:25am
   
My program is a receipt designer which makes the user designs its own receipt and put some serial numbers on that receipt. And sure, I want to make the user saves its work whenever he want and load it again. The designer panel has alot of controls, to save this panel and all its controls I must serialize all the objects within the designer panel and save it as a binary format to the file.
Suvabrata Roy 29-May-13 2:33am
   
Thanks,

Now think what you need for a control
Height , width, position, content ( for Picture box that's a stream, for other that's may some text or link ), some style info and visible or not

I don't think you need much more than that ?
what do you think ?
Rasool Ahmed 29-May-13 2:36am
   
Thanx Suvabrata Roy for replying me, I think I don't have another option, the reason of my question is to save the effort of making class and list the properties that I need for.
Suvabrata Roy 29-May-13 2:46am
   
so do you need any further help?

Have a nice time... :)

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