{"record":{"id":"d27c19d1a3dd47f3","repo":"NickeManarin/ScreenToGif","slug":"invalid-file-format-expected-png-signature-not-fo","errorCode":null,"errorMessage":"Invalid file format, expected PNG signature not found.","messagePattern":"Invalid file format, expected PNG signature not found\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"ScreenToGif.Util/Codification/Apng/Apng.cs","lineNumber":192,"sourceCode":"            var data = ms.ReadBytes(length);\n\n            if (chunkType == \"IDAT\")\n                list.Add(data);\n\n            if (chunkType == \"IEND\")\n                break;\n\n            ms.ReadUInt32();\n        }\n\n        return list;\n    }\n\n    public bool ReadFrames()\n    {\n        //Png header, 8 bytes.\n        if (!InternalStream.ReadBytes(8).SequenceEqual(new byte[] {137, 80, 78, 71, 13, 10, 26, 10}))\n            throw new Exception(\"Invalid file format, expected PNG signature not found.\");\n\n        //IHDR chunk, 25 bytes.\n        Ihdr = IhdrChunk.Read(InternalStream);\n\n        //aCTl chunk, 16 bytes.\n        Actl = ActlChunk.Read(InternalStream);\n\n        //If there's no animation control chunk, it's a normal Png.\n        if (Actl == null)\n            return false;\n\n        var masterSequence = 0;\n        var frameGroupId = -1;\n\n        //Read frames.\n        while (InternalStream.CanRead)\n        {\n            //Tries to read any chunk, except IEND.","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif.Util/Codification/Apng/Apng.cs#L174-L210","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nif (!InternalStream.ReadBytes(8).SequenceEqual(new byte[] {137, 80, 78, 71, 13, 10, 26, 10}))\n    throw new Exception(\"Invalid file format, expected PNG signature not found.\");\n\n// after: include the actual bytes found for diagnostics, reset stream if misplaced\nvar header = InternalStream.ReadBytes(8);\nvar pngSignature = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };\nif (!header.SequenceEqual(pngSignature))\n    throw new FormatException($\"Invalid file format, expected PNG signature not found. Got: [{string.Join(\", \", header)}]\");","handlingStrategy":"validation","validationCode":"// Read and check magic bytes before calling ReadFrames\nvar header = new byte[8];\nstream.Position = 0;\nstream.Read(header, 0, 8);\nstream.Position = 0;\nvar pngSignature = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };\nif (!header.SequenceEqual(pngSignature))\n    throw new FormatException(\"Input is not a valid PNG/APNG file.\");","typeGuard":"static bool IsPngSignature(byte[] header)\n{\n    var sig = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };\n    return header.Length >= 8 && header.Take(8).SequenceEqual(sig);\n}","tryCatchPattern":"try\n{\n    apng.ReadFrames();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"PNG signature\"))\n{\n    LogWriter.Log(ex, \"Invalid file passed to APNG reader\");\n    // Skip this file or prompt user\n}","preventionTips":["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."],"tags":["apng","png","image-format","binary-parsing","validation"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}