SixLabors/ImageSharp · error · ImageFormatException
Invalid Webp data, could not read unsigned 24 bit integer.
Error message
Invalid Webp data, could not read unsigned 24 bit integer.
What it means
WebpChunkParsingUtils.ReadUInt24LittleEndian throws ImageFormatException when it cannot read 3 bytes from the stream while parsing a WebP VP8/VP8L/VP8X header field (width/height). A short read means the stream ended or is truncated mid-header, so the bitstream is incomplete. The decoder treats this as invalid content.
Solutions
- Re-obtain or re-download the file; compare length against the expected size.
- Wrap decoding in try/catch (ImageFormatException) and treat as corrupt input.
- If reading from a network stream, buffer the entire payload into a MemoryStream before decoding.
- Check upstream code doesn't consume stream bytes before passing it to the decoder.
Example fix
// before using var image = Image.Load(networkStream); // may hit short-read mid-header // after using var buffered = new MemoryStream(); networkStream.CopyTo(buffered); buffered.Position = 0; using var image = Image.Load(buffered);
Defensive patterns
Strategy: try-catch
Validate before calling
// ensure full payload before decoding
using var ms = new MemoryStream();
sourceStream.CopyTo(ms);
if (ms.Length < 30) throw new InvalidDataException("WebP too small/truncated");
ms.Position = 0; Try / catch
try { using var image = Image.Load(webpStream); }
catch (ImageFormatException ex) { log.Warn(ex, "Truncated WebP data"); return null; } Prevention
- Fully buffer network streams before decoding
- Verify download completeness (Content-Length vs actual bytes)
- Catch ImageFormatException at every untrusted-input decode site
When it happens
Trigger: Decoding a WebP whose file ends inside the VP8/VP8L frame header — ReadUInt24LittleEndian's stream.Read returns fewer than 3 bytes; called from width/height parsers during header decoding.
Common situations: Truncated downloads (interrupted transfers); files cropped by an upload size limit; streams where the caller already read part of the data; using Identify on a partially written file being downloaded.
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 chunk type.
- The embedded ANI frame resource contains an invalid seek…
- Bitmap does not have a valid format.
- Invalid BMP ICC profile.
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/93bfcb779cbfc0d2.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs:273
/// <summary>
/// Reads a unsigned 24 bit integer.
/// </summary>
/// <param name="stream">The stream to read from.</param>
/// <param name="buffer">The buffer to store the read data into.</param>
/// <returns>A unsigned 24 bit integer.</returns>
/// <exception cref="ImageFormatException">
/// Thrown if the input stream is not valid.
/// </exception>
public static uint ReadUInt24LittleEndian(BufferedReadStream stream, Span<byte> buffer)
{
if (stream.Read(buffer, 0, 3) == 3)
{
buffer[3] = 0;
return BinaryPrimitives.ReadUInt32LittleEndian(buffer);
}
throw new ImageFormatException("Invalid Webp data, could not read unsigned 24 bit integer.");
}
/// <summary>
/// Writes a unsigned 24 bit integer.
/// </summary>
/// <param name="stream">The stream to write to.</param>
/// <param name="data">The uint24 data to write.</param>
/// <exception cref="InvalidDataException">
/// Thrown if the data is not a valid unsigned 24 bit integer.
/// </exception>
public static unsafe void WriteUInt24LittleEndian(Stream stream, uint data)
{
if (data >= 1 << 24)
{
throw new InvalidDataException($"Invalid data, {data} is not a unsigned 24 bit integer.");
}
uint* ptr = &data;View on GitHub (pinned to 59ce6af6fc)