{"record":{"id":"3035b818040e2802","repo":"SixLabors/ImageSharp","slug":"png-image-must-contain-a-header-chunk-and-it-must-be-located","errorCode":null,"errorMessage":"PNG Image must contain a header chunk and it must be located before any other chunks.","messagePattern":"PNG Image must contain a header chunk and it must be located before any other chunks\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Png/PngThrowHelper.cs","lineNumber":15,"sourceCode":"// Copyright (c) Six Labors.\n// Licensed under the Six Labors Split License.\n\nusing System.Diagnostics.CodeAnalysis;\nusing System.Runtime.CompilerServices;\n\nnamespace SixLabors.ImageSharp.Formats.Png;\n\ninternal static class PngThrowHelper\n{\n    [DoesNotReturn]\n    public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);\n\n    [DoesNotReturn]\n    public static void ThrowInvalidHeader() => throw new InvalidImageContentException(\"PNG Image must contain a header chunk and it must be located before any other chunks.\");\n\n    [DoesNotReturn]\n    public static void ThrowNoData() => throw new InvalidImageContentException(\"PNG Image does not contain a data chunk.\");\n\n    [DoesNotReturn]\n    public static void ThrowMissingDefaultData() => throw new InvalidImageContentException(\"APNG Image does not contain a default data chunk.\");\n\n    [DoesNotReturn]\n    public static void ThrowInvalidAnimationControl() => throw new InvalidImageContentException(\"APNG Image must contain a acTL chunk and it must be located before any IDAT and fdAT chunks.\");\n\n    [DoesNotReturn]\n    public static void ThrowMissingFrameControl() => throw new InvalidImageContentException(\"One of APNG Image's frames do not have a frame control chunk.\");\n\n    [DoesNotReturn]\n    public static void ThrowMissingPalette() => throw new InvalidImageContentException(\"PNG Image does not contain a palette chunk.\");\n\n    [DoesNotReturn]\n    public static void ThrowInvalidChunkType() => throw new InvalidImageContentException(\"Invalid PNG data.\");","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Png/PngThrowHelper.cs#L1-L33","documentation":"Thrown when the decoder never encounters a PNG IHDR (header) chunk, or encounters it after other chunks. The IHDR chunk is mandatory and per the PNG specification must be the first chunk in the file; without it the decoder cannot determine dimensions, bit depth, or color type.","triggerScenarios":"Decoding a stream whose first chunk is not IHDR (e.g. missing signature/header, data starting with other chunk types), or a file where IHDR was stripped or relocated after other chunks.","commonSituations":"Truncated downloads that cut off the header, custom chunk writers emitting ancillary chunks before IHDR, files that are actually APNG/Cairo ' png' variants or other formats mislabeled as PNG.","solutions":["Check that the file begins with the PNG signature (137 80 78 71 13 10 26 10) followed immediately by an IHDR chunk.","Re-export or re-save the image from the original source or a different encoder.","If you generate PNGs programmatically, write IHDR as the first chunk after the signature.","Catch InvalidImageContentException to detect malformed uploads."],"exampleFix":"// before (custom writer)\nWriteChunk(stream, \"gAMA\", ...);\nWriteChunk(stream, \"IHDR\", header);\n// after\nWriteChunk(stream, \"IHDR\", header);\nWriteChunk(stream, \"gAMA\", ...);","handlingStrategy":"validation","validationCode":"static bool HasIhdrFirst(byte[] png) =>\n    png.Length >= 16 &&\n    png.AsSpan(0, 8).SequenceEqual(stackalloc byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }) &&\n    png[12] == (byte)'I' && png[13] == (byte)'H' && png[14] == (byte)'D' && png[15] == (byte)'R';","typeGuard":null,"tryCatchPattern":"try\n{\n    using var image = Image.Load(stream);\n}\ncatch (InvalidImageContentException ex) when (ex.Message.Contains(\"header chunk\"))\n{\n    // missing/misordered IHDR — reject the file\n}","preventionTips":["Always write IHDR as the first chunk when generating PNGs.","Verify file completeness (IEND present) before decoding.","Detect the real format with Image.DetectFormat before assuming PNG."],"tags":["png","ihdr","missing-chunk","image-decoding"],"backgroundTag":"invalid-image-content","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}