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
- Verify the input file is actually a PNG/APNG by checking the file extension and magic bytes before calling ReadFrames.
- Ensure the stream position is at 0 (or at the start of PNG data) before calling ReadFrames.
- Validate the file is complete and not truncated by checking file size against expected size.
- 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
- Validate the file extension and magic bytes before passing to the APNG reader.
- Ensure the stream position is at 0 before calling ReadFrames.
- Use a dedicated file-type detection library (e.g., check first 8 bytes) at the import boundary.
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
- Missing IHDR chunk.
- Only 8, 24 and 32 bpp images are supported.
- Only 24 and 32 bpp images are supported.
- File is empty
- Index out of range while trying to save the resource diction
AI-assisted analysis of NickeManarin/ScreenToGif@a4d0a67c21 (2026-08-13).
Data as JSON: /api/errors/d27c19d1a3dd47f3.
Report an issue: GitHub.