private async static Task WriteHeaderChunk(PngHeader header)
        {
            var chunkData = new byte[13];

            WriteInteger(chunkData, 0, header.Width);
            WriteInteger(chunkData, 4, header.Height);

            chunkData[8] = header.BitDepth;
            chunkData[9] = header.ColorType;
            chunkData[10] = header.CompressionMethod;
            chunkData[11] = header.FilterMethod;
            chunkData[12] = header.InterlaceMethod;

            await WriteChunk(PngChunkTypes.Header, chunkData);
        }
        /// <summary>
        /// Write and PNG file out to a file stream.  Currently compression is not supported.
        /// </summary>
        /// <param name="image">The WriteableBitmap to work on.</param>
        /// <param name="stream">The destination file stream.</param>
        /// <param name="compression">Level of compression to use (-1=auto, 0=none, 1-100 is percentage).</param>
        public async static Task WritePNG(WriteableBitmap image, System.IO.Stream stream, int compression)
        {
            // Set the global class variables for the image and stream.
            _image = image;
            _stream = stream;

            // Write the png header.
            stream.Write(
                new byte[] 
                { 
                    0x89, 0x50, 0x4E, 0x47, 
                    0x0D, 0x0A, 0x1A, 0x0A 
                }, 0, 8);

            // Set the PNG header values for this image.
            var header = new PngHeader
            {
                Width = image.PixelWidth,
                Height = image.PixelHeight,
                ColorType = 6,
                BitDepth = 8,
                FilterMethod = 0,
                CompressionMethod = 0,
                InterlaceMethod = 0
            };

            // Write out the header.
            await WriteHeaderChunk(header);
            // Write out the rest of the mandatory fields to the PNG.
            await WritePhysicsChunk();
            await WriteGammaChunk();

            // Currently only uncompressed PNG's are supported, so this if statement really doesn't do anything ;).
            if (compression == -1)
            {
                // Autodetect compression setting
                await WriteDataChunksUncompressed();
            }
            else if (compression == 0)
            {
                // Write PNG without any compression
                await WriteDataChunksUncompressed();
            }
            else
            {
                // Write the PNG with a desired compression level
                await WriteDataChunks(compression);
            }

            // Write out the end of the PNG.
            await WriteEndChunk();

            // Flush the stream to make sure it's all written.
            await stream.FlushAsync();
        }