SixLabors/ImageSharp · error · UnknownImageFormatException
Cannot identify image format from empty data.
Error message
Cannot identify image format from empty data.
What it means
Identify could not inspect image metadata because the supplied byte buffer is empty. ImageSharp throws UnknownImageFormatException before attempting any decoder probing, since an empty span cannot contain a recognizable image header. This surfaces the real problem (no input) instead of a confusing decoder failure.
Solutions
- Check data.IsEmpty before calling Identify and treat it as invalid input.
- Confirm the reader that produced the buffer read the expected number of bytes.
- Handle empty payloads explicitly as 'not an image' in the caller.
Example fix
// before
var info = Image.Identify(options, buffer);
// after
if (buffer.IsEmpty)
{
return null; // no image data available
}
var info = Image.Identify(options, buffer); Defensive patterns
Strategy: validation
Validate before calling
if (buffer.Length == 0) return null; // or surface 'no image'
Type guard
static bool CanIdentify(ReadOnlySpan<byte> buffer) => buffer.Length > 0;
Try / catch
try { var info = Image.Identify(options, buffer); }
catch (UnknownImageFormatException ex) when (buffer.IsEmpty) { return null; } Prevention
- Verify reads returned the expected byte counts before identifying.
- Check file size > 0 before reading files for identification.
- Handle empty uploads explicitly at the boundary (API/UI validation).
When it happens
Trigger: Calling Image.Identify(options, ReadOnlySpan<byte>) with a zero-length span, typically from a failed file read or an empty download.
Common situations: Zero-byte uploads, files created but not yet written, network streams that closed before any bytes arrived, or slicing an array with an empty range.
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 detect image format from empty data.
- Cannot load image from empty data.
- No encoder was found for extension
- No encoder was found for extension
- The ANI file does not contain any decodable animation steps.
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/9f599ff9d8574648.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Image.FromBytes.cs:78
=> Identify(DecoderOptions.Default, buffer);
/// <summary>
/// Reads the raw image information from the specified span of bytes without fully decoding it.
/// </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="ImageInfo"/>.</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 ImageInfo Identify(DecoderOptions options, ReadOnlySpan<byte> buffer)
{
Guard.NotNull(options, nameof(options));
if (buffer.IsEmpty)
{
throw new UnknownImageFormatException("Cannot identify image format from empty data.");
}
fixed (byte* ptr = buffer)
{
using UnmanagedMemoryStream stream = new(ptr, buffer.Length);
return Identify(options, stream);
}
}
/// <summary>
/// Creates a new instance of the <see cref="Image"/> class from the given byte span.
/// The pixel format is automatically determined by the decoder.
/// </summary>
/// <param name="buffer">The byte span containing encoded image data.</param>
/// <returns><see cref="Image"/>.</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>View on GitHub (pinned to 59ce6af6fc)