SixLabors/ImageSharp · error · ImageFormatException
Invalid Webp data, could not read chunk type.
Error message
Invalid Webp data, could not read chunk type.
What it means
WebpChunkParsingUtils.ReadChunkType throws ImageFormatException when the 4-byte chunk type (FourCC) cannot be read from the stream. Unknown chunk types are normally skipped, but a chunk type must still be readable to parse the RIFF structure; failing to read 4 bytes means the data is truncated or not WebP at that offset.
Solutions
- Verify file integrity and re-download; check the file starts with RIFF....WEBP.
- Catch ImageFormatException around decoding to classify input as corrupt.
- Validate chunk structure with a tool (webpinfo) to find where the file diverges from the RIFF spec.
- Ensure no other code has advanced the stream before the decoder receives it.
Example fix
// before
using var image = Image.Load(maybeWebp);
// after
using var ms = new MemoryStream();
maybeWebp.CopyTo(ms); ms.Position = 0;
try { using var image = Image.Load(ms); }
catch (ImageFormatException) { /* not valid/truncated WebP */ } Defensive patterns
Strategy: try-catch
Try / catch
try { using var image = Image.Load(stream); }
catch (ImageFormatException ex) { log.Warn(ex, "Could not read WebP chunk type — file truncated/corrupt"); } Prevention
- Re-download truncated files
- Run webpinfo on suspect inputs
- Reset stream position and avoid shared stream reads before decode
When it happens
Trigger: Decoding a WebP where the stream ends inside a chunk's FourCC field; also reached when the header structure is misplaced so the reader expects a chunk type at end-of-data.
Common situations: Truncated downloads; files where a prior chunk size was corrupted causing the parser to seek past real data; saving a partial file during an in-progress transfer.
Understand the failure class
Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.
Related errors
- Invalid Webp data, could not read chunk size.
- Invalid Webp data, could not read unsigned 24 bit integer.
- The ANI file does not contain any frame resources.
- The stream does not contain an ANI RIFF container.
- The ANI RIFF container size is invalid.
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/f9abbd45ccfae98f.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs:364
/// <summary>
/// Identifies the chunk type from the chunk.
/// </summary>
/// <param name="stream">The stream to read the data from.</param>
/// <param name="buffer">Buffer to store the data read from the stream.</param>
/// <exception cref="ImageFormatException">
/// Thrown if the input stream is not valid.
/// </exception>
public static WebpChunkType ReadChunkType(BufferedReadStream stream, Span<byte> buffer)
{
if (stream.Read(buffer) == 4)
{
return (WebpChunkType)BinaryPrimitives.ReadUInt32BigEndian(buffer);
}
// While we ignore unknown chunks we still need a to be a ble to read a chunk type
// known or otherwise from the stream.
throw new ImageFormatException("Invalid Webp data, could not read chunk type.");
}
/// <summary>
/// Reads the ICCP chunk from the stream.
/// </summary>
/// <param name="stream">The stream to decode from.</param>
/// <param name="metadata">The image metadata.</param>
/// <param name="ignoreMetadata">If true, metadata will be ignored.</param>
/// <param name="executeAncillarySegmentAction">Executes profile parsing under the decoder's integrity policy.</param>
public static void ReadIccProfile(
BufferedReadStream stream,
ImageMetadata metadata,
bool ignoreMetadata,
Action<Action> executeAncillarySegmentAction)
{
ulong chunkSize = ReadPaddedChunkSize(stream, stackalloc byte[4], true);
// ICCP precedes image/frame data. Its framing must be readable even whenView on GitHub (pinned to 59ce6af6fc)