SixLabors/ImageSharp · error · InvalidImageContentException

Gif image does not contain a Logical Screen Descriptor.

Error message

Gif image does not contain a Logical Screen Descriptor.

What it means

Thrown when the GIF decoder cannot read the Logical Screen Descriptor immediately after the header signature. A valid GIF must contain 'GIF8xa' followed by a 7-byte screen descriptor; absence means the file is too short or not a GIF. Raised as InvalidImageContentException.

Solutions

  1. Ensure the stream is rewound to position 0 and not partially consumed before passing to Image.Load
  2. Validate the file is at least ~13 bytes (header + LSD) before decoding
  3. Re-upload/re-export the image; the file is truncated
  4. Check content-type handling so non-GIF payloads are not routed to the GIF decoder

Example fix

// before
var image = Image.Load(stream);
// after
if (stream.CanSeek) stream.Position = 0;
if (stream.Length < 13) throw new InvalidDataException("Too small to be a GIF");
var image = Image.Load(stream);
Defensive patterns

Strategy: validation

Validate before calling

// Confirm GIF header + Logical Screen Descriptor are present
if (stream.CanSeek)
{
    var head = new byte[13];
    long pos = stream.Position; int n = stream.Read(head, 0, 13); stream.Position = pos;
    if (n < 13 || !System.Text.Encoding.ASCII.GetString(head, 0, 6).StartsWith("GIF")) throw new InvalidDataException("Not a complete GIF header");
}

Try / catch

try { return Image.Load(stream); }
catch (InvalidImageContentException ex) when (ex.Message.Contains("Logical Screen Descriptor")) { return RejectAsset("truncated-gif", ex); }

Prevention

When it happens

Trigger: Image.Load/Decode where after reading the GIF signature the stream cannot supply the full LogicalScreenDescriptor bytes (stream ended or bytes invalid).

Common situations: Files with a GIF magic number but truncated bodies (bad uploads), zero-byte or few-byte files, or streams consumed partially by earlier code before being passed to the decoder.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Gif/GifThrowHelper.cs:15

// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Gif;

internal static class GifThrowHelper
{
    [DoesNotReturn]
    public static void ThrowInvalidImageContentException(string errorMessage)
        => throw new InvalidImageContentException(errorMessage);

    [DoesNotReturn]
    public static void ThrowNoHeader() => throw new InvalidImageContentException("Gif image does not contain a Logical Screen Descriptor.");

    [DoesNotReturn]
    public static void ThrowNoData() => throw new InvalidImageContentException("Unable to read Gif image data");
}

View on GitHub (pinned to 59ce6af6fc)