SixLabors/ImageSharp · error · UnknownImageFormatException
Cannot load image from empty data.
Error message
Cannot load image from empty data.
What it means
Image.Load cannot decode an image because the supplied byte buffer contains no bytes. ImageSharp throws UnknownImageFormatException up front, since format detection and decoding both require at least a header. Loading from an empty span can never succeed.
Solutions
- Check buffer.IsEmpty before calling Load and fail fast with a clear application-level error.
- Verify the producer of the buffer (file read, HTTP client, stream copy) actually returned bytes.
- Return a user-facing 'file is empty/corrupt' message instead of retrying the load.
Example fix
// before
using var image = Image.Load(options, buffer);
// after
if (buffer.IsEmpty)
{
throw new InvalidDataException("Image data is empty.");
}
using var image = Image.Load(options, buffer); Defensive patterns
Strategy: validation
Validate before calling
if (buffer.IsEmpty) throw new InvalidDataException("Image bytes are required."); Type guard
static bool IsLoadable(ReadOnlySpan<byte> buffer) => !buffer.IsEmpty;
Try / catch
try { using var image = Image.Load(options, buffer); }
catch (UnknownImageFormatException ex) when (buffer.IsEmpty) { /* report empty input */ } Prevention
- Confirm the data source (file, blob, HTTP) returned bytes before loading.
- Check file.Length or blob length before reading.
- Fail fast on zero-byte inputs in ingestion pipelines.
When it happens
Trigger: Calling Image.Load(options, ReadOnlySpan<byte>) with a zero-length span, e.g. after a failed read or an empty byte[] slice.
Common situations: Empty file downloads, database blobs that were never populated, or a Stream.CopyTo that copied 0 bytes before decoding.
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 identify image format 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/0ef39de1614a5bf3.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Image.FromBytes.cs:118
/// <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="options">The general decoder options.</param>
/// <param name="buffer">The byte span containing encoded image data.</param>
/// <returns><see cref="Image"/>.</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 Image Load(DecoderOptions options, ReadOnlySpan<byte> buffer)
{
Guard.NotNull(options, nameof(options));
if (buffer.IsEmpty)
{
throw new UnknownImageFormatException("Cannot load image from empty data.");
}
fixed (byte* ptr = buffer)
{
using UnmanagedMemoryStream stream = new(ptr, buffer.Length);
return Load(options, stream);
}
}
/// <summary>
/// Creates a new instance of the <see cref="Image{TPixel}"/> class from the given byte span.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="data">The byte span containing encoded image data.</param>
/// <returns><see cref="Image{TPixel}"/>.</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)