public void SaveApng(string file, Png [] frames, ushort delay_num, ushort delay_density) { using (var w = new BinaryWriter(new FileStream(file, FileMode.Create))) { w.Write(signature); PngIHDRChunk ihdr = (PngIHDRChunk)chunks.Find(d => d.IsType("IHDR")); w.Write(ihdr.ToBytes()); w.Write(new PngacTLChunk((uint)(frames.Length + 1), 0).ToBytes()); uint sequence = 0; w.Write(new PngfcTLChunk(ihdr, sequence++, delay_num, delay_density).ToBytes()); foreach (var chunk in chunks.FindAll(d => d.IsType("IDAT"))) { w.Write(chunk.ToBytes()); } foreach (Png frame in frames) { w.Write(new PngfcTLChunk(ihdr, sequence++, delay_num, delay_density).ToBytes()); foreach (var chunk in frame.chunks.FindAll(d => d.IsType("IDAT"))) { w.Write(((PngIDATChunk)chunk).TofdATChunk(sequence++).ToBytes()); } } w.Write(chunks.Find(d => d.IsType("IEND")).ToBytes()); } }
private PngChunk ReadChunk(BinaryReader reader, int size) { byte[] type = reader.ReadBytes(4); string _type = Encoding.ASCII.GetString(type); PngChunk chunk; byte[] data = reader.ReadBytes(size); switch (_type) { case "IHDR": chunk = new PngIHDRChunk(data); break; case "IDAT": chunk = new PngIDATChunk(data); break; default: chunk = new PngChunk(type, data); break; } uint read_crc = (uint)ToInteger(reader.ReadBytes(4)); uint calculated_crc = chunk.Crc(); if (read_crc != calculated_crc) { throw new Exception("CRCエラー"); } return(chunk); }
public PngfcTLChunk(PngIHDRChunk ihdr, uint sequence, ushort delay_num, ushort delay_density) : base("fcTL", new byte[26]) { Array.Copy(ToBytes(sequence), 0, Data, 0, 4); Array.Copy(ihdr.WidthBytes, 0, Data, 4, 4); Array.Copy(ihdr.HeightBytes, 0, Data, 8, 4); Array.Copy(ToBytes(delay_num), 0, Data, 20, 2); Array.Copy(ToBytes(delay_density), 0, Data, 22, 2); }