SixLabors/ImageSharp · error · UnknownImageFormatException
Cannot detect image format from empty data.
Error message
Cannot detect image format from empty data.
What it means
DetectFormat could not determine the image format because the supplied byte buffer contains no data. ImageSharp throws UnknownImageFormatException eagerly so the caller learns the input is empty before any decoder probing occurs. An empty span cannot match any format signature.
Solutions
- Check buffer.IsEmpty (or length == 0) before calling DetectFormat and handle it as invalid input.
- Verify the code path that filled the buffer actually read bytes (check the return value of Read/ReadAtLeast).
- If empty data is expected to be valid, treat it as 'no image' at the application level rather than retrying.
Example fix
// before
var format = Image.DetectFormat(options, buffer);
// after
if (buffer.IsEmpty)
{
throw new InvalidDataException("No image data was provided.");
}
var format = Image.DetectFormat(options, buffer); Defensive patterns
Strategy: validation
Validate before calling
if (buffer.IsEmpty) throw new ArgumentException("Image data must not be empty.", nameof(buffer)); Type guard
static bool HasImageData(ReadOnlySpan<byte> buffer) => !buffer.IsEmpty;
Try / catch
try { var format = Image.DetectFormat(options, buffer); }
catch (UnknownImageFormatException ex) when (buffer.IsEmpty) { /* treat as no image */ } Prevention
- Check span/array length before any Image API call.
- Log the byte count produced by the read step to catch truncated reads early.
- Treat empty payloads as a distinct input class in your pipeline.
When it happens
Trigger: Calling Image.DetectFormat(options, ReadOnlySpan<byte>) with a span of length 0, e.g. after reading zero bytes from a file or network stream.
Common situations: Reading a zero-byte file, an HTTP response with an empty body, a truncated download, or a memory stream positioned past its data before copying to a buffer.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Cannot identify image format from empty data.
- Cannot load image from empty data.
- Must not be empty.
- No encoder was found for extension
- No encoder was found for extension
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/bdbe13c0c82b7a50.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Image.FromBytes.cs:41
=> DetectFormat(DecoderOptions.Default, buffer);
/// <summary>
/// By reading the header on the provided byte span this calculates the images format.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="buffer">The byte span containing encoded image data to read the header from.</param>
/// <returns>The <see cref="IImageFormat"/>.</returns>
/// <exception cref="ArgumentNullException">The options are null.</exception>
/// <exception cref="NotSupportedException">The image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static unsafe IImageFormat DetectFormat(DecoderOptions options, ReadOnlySpan<byte> buffer)
{
Guard.NotNull(options, nameof(options));
if (buffer.IsEmpty)
{
throw new UnknownImageFormatException("Cannot detect image format from empty data.");
}
fixed (byte* ptr = buffer)
{
using UnmanagedMemoryStream stream = new(ptr, buffer.Length);
return DetectFormat(options, stream);
}
}
/// <summary>
/// Reads the raw image information from the specified stream without fully decoding it.
/// </summary>
/// <param name="buffer">The byte array containing encoded image data to read the header from.</param>
/// <returns>The <see cref="ImageInfo"/>.</returns>
/// <exception cref="NotSupportedException">The image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static ImageInfo Identify(ReadOnlySpan<byte> buffer)View on GitHub (pinned to 59ce6af6fc)