SixLabors/ImageSharp · error · ImageFormatException
Invalid gif colormap size
Error message
Invalid gif colormap size '{result.GlobalColorTableSize}' What it means
Thrown when parsing the GIF Logical Screen Descriptor if the computed global color table size exceeds the maximum the format and this decoder allow (255 * 4 entries). A larger value indicates corrupt packed-field bits or a non-conformant file. Raised as ImageFormatException during header parsing.
Solutions
- Re-obtain the GIF from a known-good source; the header packed field is corrupt
- Validate the file (e.g. magick identify) before processing to reject malformed inputs early
- Catch ImageFormatException at your ingest boundary and quarantine the file
- If files come from an untrusted pipeline, sanitize/re-encode them before ImageSharp decode
Example fix
// before
var image = Image.Load(stream);
// after
try { var image = Image.Load(stream); }
catch (ImageFormatException ex) { quarantine(path, ex); } Defensive patterns
Strategy: validation
Validate before calling
// Check the global color table size bits in the packed field before decode
if (stream.CanSeek)
{
var head = new byte[13]; long pos = stream.Position; stream.Read(head, 0, 13); stream.Position = pos;
if ((head[10] & 0x80) != 0 && ((head[10] & 0x07) > 7)) throw new InvalidDataException("Malformed GIF color table size");
}
Try / catch
try { return Image.Load(stream); }
catch (ImageFormatException ex) when (ex.Message.StartsWith("Invalid gif colormap size")) { Quarantine(path, ex); return null; } Prevention
- Quarantine files failing header validation instead of crashing batch jobs
- Re-encode untrusted GIFs before decoding
- Validate uploads with magick identify or Image.Identify first
- Treat this as corruption — always re-fetch from source
When it happens
Trigger: Image.Load/Decode on a GIF whose packed field's global-color-table-size bits decode to a value greater than 255*4 — a corrupt or maliciously malformed header.
Common situations: Bit-flipped/corrupt headers from damaged storage, hand-crafted fuzzed GIFs, or files passed through transformers that mangle the packed byte.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Gif image does not contain a Logical Screen Descriptor.
- Invalid EXR image header
- Unexpected end of stream while reading gif application…
- Unable to read Gif image data
- The ANI file does not contain any frame resources.
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/afaff05c7b6201fc.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs:98
/// Gets the color depth, in number of bits per pixel.
/// The lowest 3 packed bits represent the bit depth minus 1.
/// </summary>
public int BitsPerPixel => (this.Packed & 0x07) + 1;
public void WriteTo(Span<byte> buffer)
{
ref GifLogicalScreenDescriptor dest = ref Unsafe.As<byte, GifLogicalScreenDescriptor>(ref MemoryMarshal.GetReference(buffer));
dest = this;
}
public static GifLogicalScreenDescriptor Parse(ReadOnlySpan<byte> buffer)
{
GifLogicalScreenDescriptor result = MemoryMarshal.Cast<byte, GifLogicalScreenDescriptor>(buffer)[0];
if (result.GlobalColorTableSize > 255 * 4)
{
throw new ImageFormatException($"Invalid gif colormap size '{result.GlobalColorTableSize}'");
}
return result;
}
public static byte GetPackedValue(bool globalColorTableFlag, int colorResolution, bool sortFlag, int globalColorTableSize)
{
/*
Global Color Table Flag | 1 Bit
Color Resolution | 3 Bits
Sort Flag | 1 Bit
Size of Global Color Table | 3 Bits
*/
byte value = 0;
if (globalColorTableFlag)
{View on GitHub (pinned to 59ce6af6fc)