NickeManarin/ScreenToGif · error · Exception

Missing IHDR chunk.

Error message

Missing IHDR chunk.

What it means

Thrown by IhdrChunk.Read when the chunk type string (bytes 5-8 after the 4-byte length prefix) is not 'IHDR'. Per the PNG spec, the IHDR chunk must immediately follow the 8-byte PNG signature. A mismatch means the file is malformed or the stream position is wrong.

Source

Thrown at ScreenToGif.Util/Codification/Apng/Chunks/IhdrChunk.cs:37

    internal byte CompressionMethod { get; private set; }

    internal byte FilterMethod { get; private set; }

    internal byte InterlaceMethod { get; private set; }
        
    /// <summary>
    /// Attempts to read 25 bytes of the stream.
    /// </summary>
    internal static IhdrChunk Read(Stream stream)
    {
        var chunk = new IhdrChunk
        {
            Length = BitHelper.ConvertEndian(stream.ReadUInt32()), //Chunk length, 4 bytes.
            ChunkType = Encoding.ASCII.GetString(stream.ReadBytes(4)) //Chunk type, 4 bytes.
        };

        if (chunk.ChunkType != "IHDR")
            throw new Exception("Missing IHDR chunk.");

        //var pos = stream.Position;
        //chunk.ChunkData = stream.ReadBytes(chunk.Length);
        //stream.Position = pos;

        //Chunk details + CRC, 13 bytes + 4 bytes.
        chunk.Width = BitHelper.ConvertEndian(stream.ReadUInt32());
        chunk.Height = BitHelper.ConvertEndian(stream.ReadUInt32());
        chunk.BitDepth = (byte) stream.ReadByte();
        chunk.ColorType = (byte) stream.ReadByte();
        chunk.CompressionMethod = (byte) stream.ReadByte();
        chunk.FilterMethod = (byte) stream.ReadByte();
        chunk.InterlaceMethod = (byte) stream.ReadByte();
        chunk.Crc = BitHelper.ConvertEndian(stream.ReadUInt32());

        return chunk;
    }

View on GitHub (pinned to a4d0a67c21)

Solutions

  1. Re-export or re-download the PNG/APNG file from the source.
  2. Verify the stream is positioned exactly at byte offset 8 (immediately after the PNG signature) before calling IhdrChunk.Read.
  3. Use a PNG validation tool (e.g., pngcheck) to inspect the file's chunk structure.
  4. Handle the exception and skip/reject the frame rather than crashing the entire encoding pipeline.

Example fix

// before
if (chunk.ChunkType != "IHDR")
    throw new Exception("Missing IHDR chunk.");

// after: include stream position and chunk type for diagnostics
if (chunk.ChunkType != "IHDR")
    throw new FormatException($"Missing IHDR chunk. Found '{chunk.ChunkType}' at stream position {stream.Position}.");
Defensive patterns

Strategy: validation

Validate before calling

// After reading the PNG signature, peek at the chunk type
stream.Position = 8;
stream.ReadUInt32(); // length
var chunkTypeBytes = new byte[4];
stream.Read(chunkTypeBytes, 0, 4);
stream.Position = 8; // reset
var chunkType = Encoding.ASCII.GetString(chunkTypeBytes);
if (chunkType != "IHDR")
    throw new FormatException($"Expected IHDR chunk, found '{chunkType}'");

Type guard

static bool HasValidIhdr(Stream stream)
{
    if (stream.Length < 25) return false;
    var pos = stream.Position;
    stream.Position = 12; // 8 (sig) + 4 (length)
    var type = Encoding.ASCII.GetString(stream.ReadBytes(4));
    stream.Position = pos;
    return type == "IHDR";
}

Try / catch

try
{
    Ihdr = IhdrChunk.Read(InternalStream);
}
catch (Exception ex) when (ex.Message.Contains("IHDR"))
{
    LogWriter.Log(ex, "Corrupt or invalid PNG: missing IHDR chunk");
    return false; // treat as non-APNG
}

Prevention

When it happens

Trigger: After reading the 8-byte PNG header successfully, the next 4 bytes decoded as ASCII do not spell 'IHDR'. This happens with truncated PNGs, files with extra bytes between the header and IHDR, or non-standard PNG-like formats.

Common situations: PNG file is truncated and only contains the signature. An APNG that was incorrectly assembled by the encoder. Extra metadata or bytes were prepended before the actual PNG data (e.g., in a container format). Stream position was off by a few bytes from a prior read error.

Related errors


AI-assisted analysis of NickeManarin/ScreenToGif@a4d0a67c21 (2026-08-13). Data as JSON: /api/errors/23fbae31ff275d94. Report an issue: GitHub.