public static void Encode(WriteableBitmap image, Stream stream) { _image = image; _stream = stream; // Write the png header. stream.Write( new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, 0, 8); PngHeader header = new PngHeader(); header.Width = image.PixelWidth; header.Height = image.PixelHeight; header.ColorType = 6; header.BitDepth = 8; header.FilterMethod = 0; header.CompressionMethod = 0; header.InterlaceMethod = 0; WriteHeaderChunk(header); WritePhysicsChunk(); WriteGammaChunk(); if (IsWritingUncompressed) { WriteDataChunksFast(); } else { WriteDataChunks(); } WriteEndChunk(); stream.Flush(); }
private static void WriteHeaderChunk(PngHeader header) { byte[] 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; 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 void Write() { // 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. PngHeader header = new PngHeader { Width = _bitmap.PixelWidth, Height = _bitmap.PixelHeight, ColorType = 6, BitDepth = 8, FilterMethod = 0, CompressionMethod = 0, InterlaceMethod = 0 }; // Write out the header. WriteHeaderChunk( header ); // Write out the rest of the mandatory fields to the PNG. WritePhysicsChunk(); WriteGammaChunk(); // Write PNG without any compression WriteDataChunksUncompressed(); // Write out the end of the PNG. WriteEndChunk(); // Flush the stream to make sure it's all written. _stream.Flush(); }