I am getting an Exception:
An exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll but was not handled in user code
Additional information: Type 'Windows.UI.Xaml.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
at serializer.WriteObject(stream1, record1)
The class is:
[DataContract(Namespace = "http://www.abcdefg.com")]
internal class Record
{
private double n1;
private BitmapImage bimg;
internal Record(double n1, BitmapImage bimg)
{
this.n1 = n1;
this.bimg = bimg;
}
[DataMember]
internal BitmapImage image
{
get { return bimg; }
set { bimg = value; }
}
[DataMember]
internal double OperandNumberOne
{
get { return n1; }
set { n1 = value; }
}
public override string ToString()
{
return string.Format("Record: {0} and an image: {1}", n1, bimg.ToString());
}
}
and the Serialization is done here:
private async void save_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
openPicker.FileTypeFilter.Add(".jpg");
StorageFile file = await openPicker.PickSingleFileAsync();
IRandomAccessStream imageStream = await file.OpenReadAsync();
bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
ImgUIelement.Source= bitmapImage;
MemoryStream stream1 = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(Record));
Record record1 = new Record(1, bitmapImage);
serializer.WriteObject(stream1, record1);
tbx_OP.Text = "Written to Stream:" + record1.ToString();
FileSavePicker fileSavePicker = new FileSavePicker();
fileSavePicker.DefaultFileExtension = ".bla";
fileSavePicker.FileTypeChoices.Add("Bla Files", new List<string> { ".bla" });
fileSavePicker.SuggestedFileName = "New Bla File";
fileSavePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
var sfile = await fileSavePicker.PickSaveFileAsync();
using (Stream x = await sfile.OpenStreamForWriteAsync())
{
x.Seek(0, SeekOrigin.Begin);
stream1.WriteTo(x);
}
}
</string>
My requirement is to serialize an object consisting a List of Images and List of StorageFiles into a File, in my Windows Store App. I am trying to find intially whether it is possible to serialize a Single Image.
If possible could anyone please provide the sample code for a class like this..
public class ExClass
{
List<Image> imgList = new List<Image>();
List<StorageFile> audList = new List<StorageFile>();
}
I did'nt find any examples for serializing Types other than the Strings,Int,Double..Primitive ones.