简体   繁体   中英

Store a Bitmap image on Windows Phone 8

I would store a List of elements but when i run my app an exception show

The text of this exception :

Type 'System.Windows.Media.Imaging.WriteableBitmap' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.

Element el = new Element();
el.Text = TextACoder.Text;
el.Date = DateTime.Now.ToShortDateString();
el.SourceImg = ImageQR.Source;

List<Element> ListElement = new List<Element>();

if(IsolatedStorageSettings.ApplicationSettings.Contains("ListEl"))
{
    ListElement = (List<Element>)IsolatedStorageSettings.ApplicationSettings["ListEl"];
    ListElement.Add(el);
    IsolatedStorageSettings.ApplicationSettings["ListEl"] = ListElement;
    IsolatedStorageSettings.ApplicationSettings.Save();
}
else
{
    ListElement.Add(el);
    IsolatedStorageSettings.ApplicationSettings["ListEl"] = ListElement;
    IsolatedStorageSettings.ApplicationSettings.Save();
}            

When you assign into IsolatedStorageSettings.ApplicationSettings it serializes the data behind the scenes to save it to a IsolatedStorageFile , and the exception is complaining that it doesn't know how to serialize the WriteableBitmap class. You can instead store a byte array of the WriteableBitmap 's data - here's how you can convert a WriteableBitmap to a byte[] and back again (note that you'll have to store the width and height separately).

Depending on what you're doing, you can add a byte[] field to your class and whenever you assign into SourceImg , also update the byte[] field. Then mark the field that backs the SourceImg property with the NonSerialized attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM