NickeManarin/ScreenToGif · error · Exception

Invalid file format, expected PNG signature not found.

Error message

Invalid file format, expected PNG signature not found.

What it means

Thrown by Apng.ReadFrames() when the first 8 bytes of the input stream do not match the PNG magic signature (0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A). Every valid PNG and APNG file must begin with this exact byte sequence per the PNG specification.

Source

Thrown at ScreenToGif.Util/Codification/Apng/Apng.cs:192

            var data = ms.ReadBytes(length);

            if (chunkType == "IDAT")
                list.Add(data);

            if (chunkType == "IEND")
                break;

            ms.ReadUInt32();
        }

        return list;
    }

    public bool ReadFrames()
    {
        //Png header, 8 bytes.
        if (!InternalStream.ReadBytes(8).SequenceEqual(new byte[] {137, 80, 78, 71, 13, 10, 26, 10}))
            throw new Exception("Invalid file format, expected PNG signature not found.");

        //IHDR chunk, 25 bytes.
        Ihdr = IhdrChunk.Read(InternalStream);

        //aCTl chunk, 16 bytes.
        Actl = ActlChunk.Read(InternalStream);

        //If there's no animation control chunk, it's a normal Png.
        if (Actl == null)
            return false;

        var masterSequence = 0;
        var frameGroupId = -1;

        //Read frames.
        while (InternalStream.CanRead)
        {
            //Tries to read any chunk, except IEND.

View on GitHub (pinned to a4d0a67c21)

Solutions

  1. Verify the input file is actually a PNG/APNG by checking the file extension and magic bytes before calling ReadFrames.
  2. Ensure the stream position is at 0 (or at the start of PNG data) before calling ReadFrames.
  3. Validate the file is complete and not truncated by checking file size against expected size.
  4. If importing from a different format, convert to PNG first using an image library.

Example fix

// before
if (!InternalStream.ReadBytes(8).SequenceEqual(new byte[] {137, 80, 78, 71, 13, 10, 26, 10}))
    throw new Exception("Invalid file format, expected PNG signature not found.");

// after: include the actual bytes found for diagnostics, reset stream if misplaced
var header = InternalStream.ReadBytes(8);
var pngSignature = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };
if (!header.SequenceEqual(pngSignature))
    throw new FormatException($"Invalid file format, expected PNG signature not found. Got: [{string.Join(", ", header)}]");
Defensive patterns

Strategy: validation

Validate before calling

// Read and check magic bytes before calling ReadFrames
var header = new byte[8];
stream.Position = 0;
stream.Read(header, 0, 8);
stream.Position = 0;
var pngSignature = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };
if (!header.SequenceEqual(pngSignature))
    throw new FormatException("Input is not a valid PNG/APNG file.");

Type guard

static bool IsPngSignature(byte[] header)
{
    var sig = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };
    return header.Length >= 8 && header.Take(8).SequenceEqual(sig);
}

Try / catch

try
{
    apng.ReadFrames();
}
catch (Exception ex) when (ex.Message.Contains("PNG signature"))
{
    LogWriter.Log(ex, "Invalid file passed to APNG reader");
    // Skip this file or prompt user
}

Prevention

When it happens

Trigger: ReadFrames is called on a stream that contains a non-PNG file (e.g., JPEG, GIF, BMP, or a corrupted/truncated file). The stream position is not at the beginning of the PNG data. The file was partially written or downloaded.

Common situations: User selects a non-APNG image file for APNG encoding/import. A previous read operation consumed bytes and left the stream position past the header. File was downloaded incompletely or corrupted on disk. Encoding pipeline fed the wrong stream into the APNG reader.

Related errors


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