SixLabors/ImageSharp · error · ImageFormatException
Invalid Webp data, could not read chunk size.
Error message
Invalid Webp data, could not read chunk size.
What it means
WebpChunkParsingUtils.ReadPaddedChunkSize throws ImageFormatException when it cannot read the 4-byte chunk size field and the chunk is marked required. RIFF/WebP chunk sizes are padded to even lengths; a short read here means the stream ends before the size field completes, i.e. truncated or malformed WebP data.
Solutions
- Validate the file is complete (byte length vs expected) and re-download if truncated.
- Wrap Image.Load/Identify in try/catch (ImageFormatException) to handle corrupt input gracefully.
- Ensure the stream position is at a valid chunk boundary (chunk type + size are 8-byte aligned units).
- For network sources, fully buffer before decoding.
Example fix
// before
using var image = Image.Identify(partialStream); // throws on truncated chunk
// after
try { var info = Image.Identify(partialStream); }
catch (ImageFormatException ex) { log.Warn($"WebP truncated/corrupt: {ex.Message}"); } Defensive patterns
Strategy: try-catch
Validate before calling
// require a plausible minimum size and RIFF signature byte[] head = new byte[12]; stream.ReadExactly(head); bool ok = head[0..4] is "RIFF" && head[8..12] is "WEBP"; stream.Position = 0;
Try / catch
try { var info = await Image.IdentifyAsync(stream); }
catch (ImageFormatException ex) { log.Warn(ex, "WebP chunk size unreadable (truncated?)"); } Prevention
- Check file completeness before decode
- Never decode streams being written concurrently
- Validate RIFF/WEBP signatures on untrusted input
When it happens
Trigger: Decoding/Identify on a WebP stream that ends within a chunk header where required=true; stream.Read returns fewer than 4 bytes for the size.
Common situations: Partially downloaded WebP files; upload truncation; seeking to a wrong offset so the reader lands near end-of-stream; corrupted storage objects.
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 type.
- 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/ac69ce594102be31.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs:340
/// <summary>
/// Reads a chunk's complete padded extent without wrapping a uint-sized payload length.
/// </summary>
/// <param name="stream">The input stream.</param>
/// <param name="buffer">The four-byte size buffer.</param>
/// <param name="required">Whether an incomplete size field is an error.</param>
/// <returns>The padded extent, or remaining bytes when an optional size field is incomplete.</returns>
private static ulong ReadPaddedChunkSize(BufferedReadStream stream, Span<byte> buffer, bool required)
{
if (stream.Read(buffer) is 4)
{
uint chunkSize = BinaryPrimitives.ReadUInt32LittleEndian(buffer);
return (ulong)chunkSize + (chunkSize & 1);
}
if (required)
{
throw new ImageFormatException("Invalid Webp data, could not read chunk size.");
}
// Return the size of the remaining data in the stream.
return (ulong)stream.RemainingBytes;
}
/// <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)
{View on GitHub (pinned to 59ce6af6fc)