Esempio n. 1
0
        //        private int width;
        //        private int height;

        public void Write(int width1, int height1, Stream outputStream, Color[] colors)
        {
            width  = width1;
            height = height1;

            colorData = colors;

            //        switch (texture2D.Format)
            //        {
            //          case SurfaceFormat.Color:
            //        texture2D.GetData(colorData);
            //
            //        GetColorData(texture2D);


            outputStream.Write(HeaderChunk.PngSignature, 0, HeaderChunk.PngSignature.Length);
            var buffer1 = new HeaderChunk
            {
                Width             = (uint)width1,
                Height            = (uint)height1,
                BitDepth          = 8,
                ColorType         = colorType,
                CompressionMethod = 0,
                FilterMethod      = 0,
                InterlaceMethod   = 0
            }.Encode();

            outputStream.Write(buffer1, 0, buffer1.Length);
            var buffer2      = EncodePixelData();
            var memoryStream = new MemoryStream();

            try
            {
                using (var zlibStream = new ZlibStream(new MemoryStream(buffer2), CompressionMode.Compress))
                {
                    zlibStream.CopyTo(memoryStream);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred during DEFLATE compression.", ex);
            }

            var dataChunk = new DataChunk
            {
                Data = memoryStream.ToArray()
            };
            var buffer3 = dataChunk.Encode();

            outputStream.Write(buffer3, 0, buffer3.Length);
            var buffer4 = new EndChunk().Encode();

            outputStream.Write(buffer4, 0, buffer4.Length);
        }