private static void SerializeImages() { Image colorFontImage = LoadUwpAssetImage("ColorFont.png"); string colorFontImageSerialized = ImageSerializer.Serialize(colorFontImage); Image miniMarioImage = LoadUwpAssetImage("MiniMario.png"); string miniMarioImageSerialized = ImageSerializer.Serialize(miniMarioImage, true); }
private static void ValidateImageSerializer() { Image originalImage = LoadUwpAssetImage("ColorFont.png"); string serializedImage1 = ImageSerializer.Serialize(originalImage); string serializedImage2 = ImageSerializer.Serialize(originalImage, true); var deserializedImage1 = ImageSerializer.Deserialize(serializedImage1); var deserializedImage2 = ImageSerializer.Deserialize(serializedImage2); if (!ImagesEqual(originalImage, deserializedImage1, true) || !ImagesEqual(originalImage, deserializedImage2, false)) { throw new Exception("Image serialized failed."); } }
private object Process(object obj, Machine machine) { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); ImageSerializer serializer = new ImageSerializer(writer); serializer.Serialize(obj); writer.Close(); stream = new MemoryStream(stream.ToArray()); BinaryReader reader = new BinaryReader(stream); ImageSerializer deserializer = new ImageSerializer(reader, machine); return deserializer.Deserialize(); }
public void SerializeDeserializeObjectsWithCycle() { IObject obja = new BaseObject(null, 1); IObject objb = new BaseObject(null, 1); obja[0] = objb; objb[0] = obja; MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); ImageSerializer serializer = new ImageSerializer(writer); serializer.Serialize(obja); serializer.Serialize(objb); writer.Close(); stream = new MemoryStream(stream.ToArray()); BinaryReader reader = new BinaryReader(stream); ImageSerializer deserializer = new ImageSerializer(reader, null); var resulta = deserializer.Deserialize(); var resultb = deserializer.Deserialize(); Assert.IsNotNull(resulta); Assert.IsInstanceOfType(resulta, typeof(IObject)); var iobja = (IObject)resulta; Assert.IsNull(iobja.Behavior); Assert.AreEqual(1, iobja.NoVariables); Assert.IsNotNull(resultb); Assert.IsInstanceOfType(resultb, typeof(IObject)); var iobjb = (IObject)resultb; Assert.IsNull(iobjb.Behavior); Assert.AreEqual(1, iobjb.NoVariables); Assert.AreEqual(iobja, iobjb[0]); Assert.AreEqual(iobjb, iobja[0]); }
public static void Main(string[] args) { //// According http://msdn.microsoft.com/en-us/magazine/cc300474.aspx LifetimeServices.LeaseTime = TimeSpan.FromMinutes(10); LifetimeServices.RenewOnCallTime = TimeSpan.FromMinutes(15); LifetimeServices.SponsorshipTimeout = TimeSpan.FromMinutes(1); Machine machine = null; string imageloadname = GetImageFileName(args); if (imageloadname != null) { var stream = File.Open(imageloadname, FileMode.Open); var reader = new BinaryReader(stream); ImageSerializer serializer = new ImageSerializer(reader, null); machine = (Machine)serializer.Deserialize(); Machine.SetCurrent(machine); reader.Close(); object pgm = machine.GetGlobalObject("Program"); if (pgm != null) { IBehavior program = (IBehavior)pgm; machine.SendMessage(program, "main", null, null); return; } } else machine = new Machine(); foreach (string arg in GetFileNames(args)) { Loader ldr = new Loader(arg, new VmCompiler()); try { ldr.LoadAndExecute(machine); } catch (Exception ex) { System.Console.Error.WriteLine(ex.Message); System.Console.Error.WriteLine(ex.StackTrace); } } string imagesavename = GetOption("save", "s", args); if (imagesavename != null) { var stream = File.Open(imagesavename, FileMode.Create); var writer = new BinaryWriter(stream); ImageSerializer serializer = new ImageSerializer(writer); serializer.Serialize(machine); writer.Close(); return; } Loader loader = new Loader(System.Console.In, new VmCompiler()); while (true) { try { loader.LoadAndExecute(machine); } catch (Exception ex) { System.Console.Error.WriteLine(ex.Message); System.Console.Error.WriteLine(ex.StackTrace); } } }