{"record":{"id":"7307ee719151a0b9","repo":"SixLabors/ImageSharp","slug":"the-stream-does-not-contain-an-ani-riff-container","errorCode":null,"errorMessage":"The stream does not contain an ANI RIFF container.","messagePattern":"The stream does not contain an ANI RIFF container\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Ani/AniDecoderCore.cs","lineNumber":250,"sourceCode":"    /// </summary>\n    /// <param name=\"stream\">The ANI stream.</param>\n    private void ParseContainer(BufferedReadStream stream)\n    {\n        // Parser-owned chunk state is replaced by the container currently being scanned.\n        this.frameLists.Clear();\n        this.sequence?.Dispose();\n        this.rates?.Dispose();\n        this.sequence = null;\n        this.rates = null;\n\n        long containerStart = stream.Position;\n        Span<byte> riffHeader = this.buffer[..AniConstants.RiffHeaderSize];\n        ReadExactly(stream, riffHeader, \"RIFF header\");\n\n        if (!riffHeader[..4].SequenceEqual(AniConstants.RiffFourCc)\n            || !riffHeader.Slice(8, 4).SequenceEqual(AniConstants.AniFormTypeFourCc))\n        {\n            throw new InvalidImageContentException(\"The stream does not contain an ANI RIFF container.\");\n        }\n\n        uint declaredSize = BinaryPrimitives.ReadUInt32LittleEndian(riffHeader[4..]);\n        if (declaredSize < sizeof(uint))\n        {\n            throw new InvalidImageContentException(\"The ANI RIFF container size is invalid.\");\n        }\n\n        // RIFF size excludes the initial identifier and size field. Some real-world ANI files incorrectly\n        // include those eight bytes, so the physical stream length remains the hard read boundary.\n        long declaredEnd = checked(containerStart + 8 + declaredSize);\n        long containerEnd = Math.Min(declaredEnd, stream.Length);\n        bool headerFound = false;\n\n        while (stream.Position + AniConstants.ChunkHeaderSize <= containerEnd)\n        {\n            AniRiffChunkHeader chunk = this.ReadChunkHeader(stream);\n            long dataEnd = GetChunkDataEnd(stream, chunk.Size, containerEnd);","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Ani/AniDecoderCore.cs#L232-L268","documentation":"The ANI decoder first reads the 12-byte RIFF header and verifies the 'RIFF' FourCC and the 'ACON' (ANI form) type. If either does not match, the stream is not an ANI container and InvalidImageContentException is thrown. This is the earliest structural validation, so it usually means the stream is not ANI at all or is corrupt from byte zero.","triggerScenarios":"Calling Image.DecodeAsync/IdentifyAsync on a stream whose first 4 bytes are not 'RIFF' or whose bytes 8-12 are not the ANI form type (ACON): e.g. a plain .ico, .wav, .webp RIFF file, or a text/HTML error page saved with an .ani extension.","commonSituations":"Wrong-file-downloaded scenarios (server returns HTML with 200); confusing RIFF-family files (.wav/.webp) pointed at the ANI decoder; decoder auto-detection overridden or misconfigured.","solutions":["Confirm the file really is an ANI (first 12 bytes: 'RIFF' + size + 'ACON'); check with a hex editor or `file` command.","Re-download the file — an HTML error page or truncated transfer often replaces the real bytes.","Use Image.DetectFormatAsync before decode so a non-ANI stream is routed to the right decoder or rejected clearly."],"exampleFix":"// before\nusing Image<Rgba32> img = Image.Load<Rgba32>(path); // assumes ANI\n// after\nIImageFormat fmt = await Image.DetectFormatAsync(path);\nif (fmt?.Name != \"ANI\") throw new NotSupportedException($\"{fmt?.Name ?? \"unknown\"} is not ANI\");","handlingStrategy":"validation","validationCode":"bool IsAniContainer(Stream s)\n{\n    byte[] h = new byte[12];\n    if (s.Read(h, 0, 12) != 12) return false;\n    s.Position = 0;\n    return h.AsSpan(0, 4).SequenceEqual(\"RIFF\"u8) && h.AsSpan(8, 4).SequenceEqual(\"ACON\"u8);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call Image.DetectFormatAsync before loading with an assumed format.","Check HTTP responses: ensure you saved the binary payload, not an HTML error page.","Remember .ani is RIFF/ACON — a .wav or .webp will fail this exact check."],"tags":["ani","riff","wrong-format","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"}