/// <summary> /// Initializes a new instance of the <see cref="JPEGFile"/> class from the /// specified data stream. /// </summary> /// <param name="stream">A <see cref="Sytem.IO.Stream"/> that contains image data.</param> /// <param name="encoding">The encoding to be used for text metadata when the source encoding is unknown.</param> protected internal PNGFile(Stream stream, System.Text.Encoding encoding) { Format = ImageFileFormat.PNG; Chunks = new List <PNGChunk>(); Encoding = encoding; BitConverterEx conv = BitConverterEx.BigEndian; // Skip header stream.Seek(8, SeekOrigin.Begin); // Read chunks in order until we reach the end of file. while (stream.Position != stream.Length) { // Length of chunk data byte[] lengthBytes = new byte[4]; if (stream.Read(lengthBytes, 0, 4) != 4) { throw new NotValidPNGFileException(); } uint length = conv.ToUInt32(lengthBytes, 0); // Chunk type byte[] typeBytes = new byte[4]; if (stream.Read(typeBytes, 0, 4) != 4) { throw new NotValidPNGFileException(); } string type = Encoding.ASCII.GetString(typeBytes); // Chunk data byte[] data = Utility.GetStreamBytes(stream, length); // CRC of type name and data byte[] crcBytes = new byte[4]; if (stream.Read(crcBytes, 0, 4) != 4) { throw new NotValidPNGFileException(); } uint crc = conv.ToUInt32(crcBytes, 0); // Add to chunks list PNGChunk chunk = new PNGChunk(type, data); if (chunk.CRC != crc) { throw new NotValidPNGFileException(); } Chunks.Add(chunk); } ReadPNGMetadata(); }
/// <summary> /// Initializes a new instance of the <see cref="JPEGFile"/> class from the /// specified data stream. /// </summary> /// <param name="stream">A <see cref="Sytem.IO.Stream"/> that contains image data.</param> /// <param name="encoding">The encoding to be used for text metadata when the source encoding is unknown.</param> protected internal PNGFile(Stream stream, System.Text.Encoding encoding) { Format = ImageFileFormat.PNG; Chunks = new List<PNGChunk>(); Encoding = encoding; BitConverterEx conv = BitConverterEx.BigEndian; // Skip header stream.Seek(8, SeekOrigin.Begin); // Read chunks in order until we reach the end of file. while (stream.Position != stream.Length) { // Length of chunk data byte[] lengthBytes = new byte[4]; if (stream.Read(lengthBytes, 0, 4) != 4) throw new NotValidPNGFileException(); uint length = conv.ToUInt32(lengthBytes, 0); // Chunk type byte[] typeBytes = new byte[4]; if (stream.Read(typeBytes, 0, 4) != 4) throw new NotValidPNGFileException(); string type = Encoding.ASCII.GetString(typeBytes); // Chunk data byte[] data = Utility.GetStreamBytes(stream, length); // CRC of type name and data byte[] crcBytes = new byte[4]; if (stream.Read(crcBytes, 0, 4) != 4) throw new NotValidPNGFileException(); uint crc = conv.ToUInt32(crcBytes, 0); // Add to chunks list PNGChunk chunk = new PNGChunk(type, data); if (chunk.CRC != crc) throw new NotValidPNGFileException(); Chunks.Add(chunk); } ReadPNGMetadata(); }