{"record":{"id":"0ae33aff70a41ebb","repo":"SixLabors/ImageSharp","slug":"errormessage-pngthrowhelper","errorCode":null,"errorMessage":"{errorMessage}","messagePattern":"\\{errorMessage\\}","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Png/PngThrowHelper.cs","lineNumber":12,"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.\");","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Png/PngThrowHelper.cs#L1-L30","documentation":"This is the generic PNG content-error entry point in PngThrowHelper. SixLabors.ImageSharp throws InvalidImageContentException when the PNG stream being decoded violates the PNG specification in a way not covered by a more specific helper, so the decoder aborts rather than return a corrupt image. The message is supplied by the caller, so read the exception message for the exact problem.","triggerScenarios":"Any call to Image.Load/LoadAsync, Image.Load<TPixel>, or decoding via PngDecoder where a chunk fails validation and the decoder calls ThrowInvalidImageContentException(errorMessage) with a decoder-specific message.","commonSituations":"Feeding truncated or hand-edited PNG files, streams containing non-PNG data renamed to .png, files corrupted in transfer, or re-encoded images with spec-violating chunk contents.","solutions":["Read the exception message to identify the specific PNG violation, then fix or regenerate the source image.","Verify the file is a genuine PNG (starts with the 8-byte PNG signature) and is not truncated.","Validate the image with an external tool (e.g. pngcheck) before loading.","Wrap the load call in a try-catch for InvalidImageContentException and handle untrusted/corrupt input gracefully."],"exampleFix":"// before\nusing var image = Image.Load(stream);\n// after\ntry\n{\n    using var image = Image.Load(stream);\n}\ncatch (InvalidImageContentException ex)\n{\n    // log ex.Message and treat input as corrupt\n}","handlingStrategy":"try-catch","validationCode":"if (span.Length < 8 || !span.Slice(0, 8).SequenceEqual(new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }))\n    throw new InvalidOperationException(\"Not a PNG file\");","typeGuard":"static bool IsPng(Stream s)\n{\n    Span<byte> sig = stackalloc byte[8];\n    return s.Read(sig) == 8 && sig.SequenceEqual(stackalloc byte[] { 137, 80, 78, 71, 13, 10, 26, 10 });\n}","tryCatchPattern":"try\n{\n    using var image = Image.Load(stream);\n}\ncatch (InvalidImageContentException ex)\n{\n    logger.LogWarning(ex, \"Rejecting corrupt PNG input\");\n}","preventionTips":["Check the PNG signature before decoding untrusted input.","Run pngcheck on files that come from external sources.","Use checksums/hashes when transferring images over the network."],"tags":["png","image-decoding","corrupt-input","imagesharp"],"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"}