{"record":{"id":"97a42a73e23e293d","repo":"SixLabors/ImageSharp","slug":"tga-image-does-not-have-a-valid-format","errorCode":null,"errorMessage":"TGA image does not have a valid format.","messagePattern":"TGA image does not have a valid format\\.","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Tga/TgaDecoderCore.cs","lineNumber":200,"sourceCode":"                        this.ReadRle(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, 4, origin);\n                    }\n                    else\n                    {\n                        this.ReadBgra32(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, origin);\n                    }\n\n                    break;\n\n                default:\n                    TgaThrowHelper.ThrowNotSupportedException(\"ImageSharp does not support this kind of tga files.\");\n                    break;\n            }\n\n            return image;\n        }\n        catch (IndexOutOfRangeException e)\n        {\n            throw new ImageFormatException(\"TGA image does not have a valid format.\", e);\n        }\n    }\n\n    /// <summary>\n    /// Reads a uncompressed TGA image with a palette.\n    /// </summary>\n    /// <typeparam name=\"TPixel\">The pixel type.</typeparam>\n    /// <param name=\"stream\">The <see cref=\"BufferedReadStream\"/> containing image data.</param>\n    /// <param name=\"width\">The width of the image.</param>\n    /// <param name=\"height\">The height of the image.</param>\n    /// <param name=\"pixels\">The <see cref=\"Buffer2D{TPixel}\"/> to assign the palette to.</param>\n    /// <param name=\"palette\">The color palette.</param>\n    /// <param name=\"colorMapPixelSizeInBytes\">Color map size of one entry in bytes.</param>\n    /// <param name=\"origin\">The image origin.</param>\n    private void ReadPaletted<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, Span<byte> palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin)\n        where TPixel : unmanaged, IPixel<TPixel>\n    {\n        bool invertX = InvertX(origin);","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs#L182-L218","documentation":"TgaDecoderCore.Decode wraps its whole decode in a catch for IndexOutOfRangeException and rethrows it as ImageFormatException with 'TGA image does not have a valid format.' This converts out-of-bounds reads caused by inconsistent TGA header/field data (offsets, color-map sizes, pixel counts that run past the buffer) into a clean format error with the original exception as InnerException. It signals the byte stream does not match any TGA layout the decoder can navigate safely.","triggerScenarios":"Image.Load/Image.Identify on a TGA file whose declared header fields (width/height, color-map length/offset, pixel depth) cause row or color-map indexing to exceed the actual data — i.e. data shorter than the header promises.","commonSituations":"Truncated TGA files from interrupted uploads; TGA variants from old/obscure tools with nonstandard field layouts; renaming non-TGA files to .tga; fuzzed or corrupted images.","solutions":["Open the InnerException/stack to see which read went out of bounds, then inspect the TGA header (18 bytes) with a hex editor for implausible dimensions or color-map values.","Re-export the image in a well-formed TGA (or convert to PNG) from the original source.","Verify the file size matches what the producer wrote; re-transfer if truncated.","Catch ImageFormatException and treat the input as invalid/unreadable rather than retrying."],"exampleFix":"// before\nusing var image = Image.Load(\"legacy.tga\");\n// after\ntry\n{\n    using var image = Image.Load(\"legacy.tga\");\n}\ncatch (ImageFormatException ex)\n{\n    Console.WriteLine($\"Invalid TGA: {ex.Message} (root: {ex.InnerException?.Message})\");\n}","handlingStrategy":"try-catch","validationCode":"// Sanity-check the 18-byte TGA header before decode.\nbyte[] h = new byte[18];\nusing (var fs = File.OpenRead(path)) if (fs.Read(h, 0, 18) != 18) throw new InvalidDataException(\"Too short for TGA header.\");\nint width = h[12] | (h[13] << 8), height = h[14] | (h[15] << 8);\nint depth = h[16];\nif (width <= 0 || height <= 0 || depth is not (8 or 15 or 16 or 24 or 32))\n    throw new InvalidDataException(\"Suspicious TGA header.\");","typeGuard":null,"tryCatchPattern":"try { using var img = Image.Load(path); }\ncatch (ImageFormatException ex)\n{\n    // InnerException holds the underlying IndexOutOfRangeException\n    Log(ex.InnerException ?? ex, \"TGA does not have a valid format.\");\n}","preventionTips":["Inspect InnerException to pinpoint which TGA field ran out of bounds.","Re-export legacy TGAs into a modern format before processing.","Verify file completeness against expected sizes for uploaded TGAs.","Prefer PNG over TGA for interchange when you control the pipeline."],"tags":["tga","image-decoding","malformed-input","index-out-of-range"],"backgroundTag":"invalid-argument-format","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"}