{"record":{"id":"e5c099262f96079a","repo":"SixLabors/ImageSharp","slug":"invalid-tiff-file-header","errorCode":null,"errorMessage":"Invalid TIFF file header.","messagePattern":"Invalid TIFF file header\\.","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs","lineNumber":26,"sourceCode":"internal static class TiffThrowHelper\n{\n    [DoesNotReturn]\n    public static Exception ThrowImageFormatException(string errorMessage) => throw new ImageFormatException(errorMessage);\n\n    [DoesNotReturn]\n    public static Exception ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);\n\n    [DoesNotReturn]\n    public static Exception NotSupportedDecompressor(string compressionType) => throw new NotSupportedException($\"Not supported decoder compression method: {compressionType}\");\n\n    [DoesNotReturn]\n    public static Exception NotSupportedCompressor(string compressionType) => throw new NotSupportedException($\"Not supported encoder compression method: {compressionType}\");\n\n    [DoesNotReturn]\n    public static Exception InvalidColorType(string colorType) => throw new NotSupportedException($\"Invalid color type: {colorType}\");\n\n    [DoesNotReturn]\n    public static Exception ThrowInvalidHeader() => throw new ImageFormatException(\"Invalid TIFF file header.\");\n\n    [DoesNotReturn]\n    public static void ThrowNotSupported(string message) => throw new NotSupportedException(message);\n\n    [DoesNotReturn]\n    public static void ThrowArgumentException(string message) => throw new ArgumentException(message);\n}\n","sourceCodeStart":8,"sourceCodeEnd":34,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs#L8-L34","documentation":"TiffThrowHelper.ThrowInvalidHeader throws ImageFormatException when the TIFF file header cannot be parsed. A valid TIFF starts with II/4949 or MM/4D4D byte-order marks followed by magic 42 and a valid first IFD offset; anything else is rejected as not a TIFF. This is the decoder's gatekeeper for input that is not TIFF at all.","triggerScenarios":"Calling Image.Load/Identify on a stream whose first bytes are not a TIFF header — e.g. a truncated file, an HTML error page saved with a .tiff extension, or a different format misidentified by the auto-detector.","commonSituations":"Downloads truncated at zero bytes; servers returning JSON/HTML errors where a TIFF was expected; cloud storage objects corrupted in transit; pointing the loader at the wrong file.","solutions":["Verify the file is a real TIFF (first bytes 'II' or 'MM') with a hex dump or tiffinfo.","Re-download or re-export the file; check for truncation (compare byte size with source).","If loading mixed formats, let ImageSharp's auto-detection run and handle ImageFormatException, or check the format explicitly first.","Confirm the stream position is 0 before loading — a partially consumed stream yields garbage bytes."],"exampleFix":"// before\nusing var image = Image.Load(stream); // throws if not TIFF\n// after\nif (!stream.CanSeek) { /* buffer it first */ }\nstream.Position = 0;\ntry { using var image = Image.Load(stream); }\ncatch (ImageFormatException) { /* not a TIFF / corrupt header */ }","handlingStrategy":"try-catch","validationCode":"// check magic before loading\nSpan<byte> magic = stackalloc byte[4];\nstream.ReadExactly(magic);\nbool isTiff = magic[0] is 0x49 or 0x4D && magic[1] == magic[0] && magic[2] == 42;\nstream.Position = 0;","typeGuard":"static bool LooksLikeTiff(byte[] bytes) =>\n    bytes.Length >= 4 && (bytes[0] == 'I' && bytes[1] == 'I' || bytes[0] == 'M' && bytes[1] == 'M') && bytes[2] == 42;","tryCatchPattern":"try { using var image = Image.Load(stream); }\ncatch (ImageFormatException ex) { log.Warn(ex, \"Not a valid TIFF\"); return null; }","preventionTips":["Reset stream.Position = 0 before loading","Validate magic bytes for untrusted downloads","Check file size > 0 and completeness before decoding"],"tags":["tiff","header","format-detection","corrupt-file"],"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"}