using System.Collections.Generic; using System.IO; namespace LibAPNG { /// /// Describe a single frame. /// public class Frame { public static byte[] Signature = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}; private List idatChunks = new List(); private List otherChunks = new List(); /// /// Gets or Sets the acTL chunk /// public IHDRChunk IHDRChunk { get; set; } /// /// Gets or Sets the fcTL chunk /// public fcTLChunk fcTLChunk { get; set; } /// /// Gets or Sets the IEND chunk /// public IENDChunk IENDChunk { get; set; } /// /// Gets or Sets the other chunks /// public List OtherChunks { get { return otherChunks; } set { otherChunks = value; } } /// /// Gets or Sets the IDAT chunks /// public List IDATChunks { get { return idatChunks; } set { idatChunks = value; } } /// /// Add an Chunk to end end of existing list. /// public void AddOtherChunk(OtherChunk chunk) { otherChunks.Add(chunk); } /// /// Add an IDAT Chunk to end end of existing list. /// public void AddIDATChunk(IDATChunk chunk) { idatChunks.Add(chunk); } /// /// Gets the frame as PNG FileStream. /// public MemoryStream GetStream() { var ihdrChunk = new IHDRChunk(IHDRChunk); if (fcTLChunk != null) { // Fix frame size with fcTL data. ihdrChunk.ModifyChunkData(0, Helper.ConvertEndian(fcTLChunk.Width)); ihdrChunk.ModifyChunkData(4, Helper.ConvertEndian(fcTLChunk.Height)); } // Write image data using (var ms = new MemoryStream()) { ms.WriteBytes(Signature); ms.WriteBytes(ihdrChunk.RawData); otherChunks.ForEach(o => ms.WriteBytes(o.RawData)); idatChunks.ForEach(i => ms.WriteBytes(i.RawData)); ms.WriteBytes(IENDChunk.RawData); ms.Position = 0; return ms; } } } }