SixLabors/ImageSharp · error · ImageFormatException

Invalid TIFF file header.

Error message

Invalid TIFF file header.

What it means

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.

Solutions

  1. Verify the file is a real TIFF (first bytes 'II' or 'MM') with a hex dump or tiffinfo.
  2. Re-download or re-export the file; check for truncation (compare byte size with source).
  3. If loading mixed formats, let ImageSharp's auto-detection run and handle ImageFormatException, or check the format explicitly first.
  4. Confirm the stream position is 0 before loading — a partially consumed stream yields garbage bytes.

Example fix

// before
using var image = Image.Load(stream); // throws if not TIFF
// after
if (!stream.CanSeek) { /* buffer it first */ }
stream.Position = 0;
try { using var image = Image.Load(stream); }
catch (ImageFormatException) { /* not a TIFF / corrupt header */ }
Defensive patterns

Strategy: try-catch

Validate before calling

// check magic before loading
Span<byte> magic = stackalloc byte[4];
stream.ReadExactly(magic);
bool isTiff = magic[0] is 0x49 or 0x4D && magic[1] == magic[0] && magic[2] == 42;
stream.Position = 0;

Type guard

static bool LooksLikeTiff(byte[] bytes) =>
    bytes.Length >= 4 && (bytes[0] == 'I' && bytes[1] == 'I' || bytes[0] == 'M' && bytes[1] == 'M') && bytes[2] == 42;

Try / catch

try { using var image = Image.Load(stream); }
catch (ImageFormatException ex) { log.Warn(ex, "Not a valid TIFF"); return null; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/e5c099262f96079a. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs:26

internal static class TiffThrowHelper
{
    [DoesNotReturn]
    public static Exception ThrowImageFormatException(string errorMessage) => throw new ImageFormatException(errorMessage);

    [DoesNotReturn]
    public static Exception ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);

    [DoesNotReturn]
    public static Exception NotSupportedDecompressor(string compressionType) => throw new NotSupportedException($"Not supported decoder compression method: {compressionType}");

    [DoesNotReturn]
    public static Exception NotSupportedCompressor(string compressionType) => throw new NotSupportedException($"Not supported encoder compression method: {compressionType}");

    [DoesNotReturn]
    public static Exception InvalidColorType(string colorType) => throw new NotSupportedException($"Invalid color type: {colorType}");

    [DoesNotReturn]
    public static Exception ThrowInvalidHeader() => throw new ImageFormatException("Invalid TIFF file header.");

    [DoesNotReturn]
    public static void ThrowNotSupported(string message) => throw new NotSupportedException(message);

    [DoesNotReturn]
    public static void ThrowArgumentException(string message) => throw new ArgumentException(message);
}

View on GitHub (pinned to 59ce6af6fc)