public void TestImagePersistence() { // Setup the test Bitmap bitmap1 = FrameGenerator.GenerateRandomBitmap(48, 37, 0); Bitmap bitmap2 = FrameGenerator.GenerateRandomBitmap(12, 45, 0); Stream stream = new MemoryStream(); // Test the methods PersistenceHelper.SaveImageToStream(bitmap1, stream); PersistenceHelper.SaveImageToStream(bitmap2, stream); // Reset stream stream.Position = 0; // Load the bitmaps back up again one at a time Bitmap loadedBitmap1 = PersistenceHelper.LoadImageFromStream(stream); Bitmap loadedBitmap2 = PersistenceHelper.LoadImageFromStream(stream); // Compare images Assert.IsTrue(ImageUtilities.ImagesAreIdentical(bitmap1, loadedBitmap1), "An image loaded from a stream must match completely the image that was saved"); Assert.IsTrue(ImageUtilities.ImagesAreIdentical(bitmap2, loadedBitmap2), "An image loaded from a stream must match completely the image that was saved"); // Try to edit the loaded bitmaps to verify editability loadedBitmap1.SetPixel(0, 0, Color.Black); loadedBitmap2.SetPixel(0, 0, Color.Black); }
/// <summary> /// Loads a single layer from a specified stream /// </summary> /// <param name="frame">The frame to load the layer into</param> /// <param name="stream">The stream to load the layer from</param> private void LoadLayerFromStream(Frame frame, Stream stream) { // Load the layer's name string name = null; if (blockVersion >= 2) { BinaryReader reader = new BinaryReader(stream, Encoding.UTF8); name = reader.ReadString(); } Bitmap layerBitmap = PersistenceHelper.LoadImageFromStream(stream); IFrameLayer layer = frame.AddLayer(layerBitmap); // Add the attributes that were loaded earlier if (name != null) { layer.Name = name; } }
/// <summary> /// Loads a Frame from the given stream, using the specified version /// number when reading properties /// </summary> /// <param name="stream">The stream to load the frame from</param> /// <param name="owningAnimation">The Animation object that will be used to create the Frame with</param> /// <returns>The Frame object loaded</returns> protected Frame LoadFrameFromStream(Stream stream, Animation owningAnimation) { BinaryReader reader = new BinaryReader(stream); Frame frame = new Frame(owningAnimation, owningAnimation.Width, owningAnimation.Height, false); if (blockVersion == 0) { var bitmap = PersistenceHelper.LoadImageFromStream(stream); frame.SetFrameBitmap(bitmap, false); } else if (blockVersion >= 1 && blockVersion <= CurrentVersion) { LoadLayersFromStream(stream, frame); } else { throw new Exception("Unknown frame block version " + blockVersion); } frame.ID = reader.ReadInt32(); // Get the hash now int length = reader.ReadInt32(); var hash = new byte[length]; stream.Read(hash, 0, length); frame.SetHash(hash); // If the block version is prior to 1, update the frame's hash value due to the new way the hash is calculated if (blockVersion < 1) { frame.UpdateHash(); } owningAnimation.AddFrame(frame); return(frame); }