{"record":{"id":"0ef39de1614a5bf3","repo":"SixLabors/ImageSharp","slug":"cannot-load-image-from-empty-data","errorCode":null,"errorMessage":"Cannot load image from empty data.","messagePattern":"Cannot load image from empty data\\.","errorType":"exception","errorClass":"UnknownImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Image.FromBytes.cs","lineNumber":118,"sourceCode":"\n    /// <summary>\n    /// Creates a new instance of the <see cref=\"Image\"/> class from the given byte span.\n    /// The pixel format is automatically determined by the decoder.\n    /// </summary>\n    /// <param name=\"options\">The general decoder options.</param>\n    /// <param name=\"buffer\">The byte span containing encoded image data.</param>\n    /// <returns><see cref=\"Image\"/>.</returns>\n    /// <exception cref=\"ArgumentNullException\">The options are null.</exception>\n    /// <exception cref=\"NotSupportedException\">The image format is not supported.</exception>\n    /// <exception cref=\"InvalidImageContentException\">The encoded image contains invalid content.</exception>\n    /// <exception cref=\"UnknownImageFormatException\">The encoded image format is unknown.</exception>\n    public static unsafe Image Load(DecoderOptions options, ReadOnlySpan<byte> buffer)\n    {\n        Guard.NotNull(options, nameof(options));\n\n        if (buffer.IsEmpty)\n        {\n            throw new UnknownImageFormatException(\"Cannot load image from empty data.\");\n        }\n\n        fixed (byte* ptr = buffer)\n        {\n            using UnmanagedMemoryStream stream = new(ptr, buffer.Length);\n            return Load(options, stream);\n        }\n    }\n\n    /// <summary>\n    /// Creates a new instance of the <see cref=\"Image{TPixel}\"/> class from the given byte span.\n    /// </summary>\n    /// <typeparam name=\"TPixel\">The pixel format.</typeparam>\n    /// <param name=\"data\">The byte span containing encoded image data.</param>\n    /// <returns><see cref=\"Image{TPixel}\"/>.</returns>\n    /// <exception cref=\"NotSupportedException\">The image format is not supported.</exception>\n    /// <exception cref=\"InvalidImageContentException\">The encoded image contains invalid content.</exception>\n    /// <exception cref=\"UnknownImageFormatException\">The encoded image format is unknown.</exception>","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Image.FromBytes.cs#L100-L136","documentation":"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.","triggerScenarios":"Calling Image.Load(options, ReadOnlySpan<byte>) with a zero-length span, e.g. after a failed read or an empty byte[] slice.","commonSituations":"Empty file downloads, database blobs that were never populated, or a Stream.CopyTo that copied 0 bytes before decoding.","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."],"exampleFix":"// before\nusing var image = Image.Load(options, buffer);\n// after\nif (buffer.IsEmpty)\n{\n    throw new InvalidDataException(\"Image data is empty.\");\n}\nusing var image = Image.Load(options, buffer);","handlingStrategy":"validation","validationCode":"if (buffer.IsEmpty) throw new InvalidDataException(\"Image bytes are required.\");","typeGuard":"static bool IsLoadable(ReadOnlySpan<byte> buffer) => !buffer.IsEmpty;","tryCatchPattern":"try { using var image = Image.Load(options, buffer); }\ncatch (UnknownImageFormatException ex) when (buffer.IsEmpty) { /* report empty input */ }","preventionTips":["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."],"tags":["image","empty-input","decoding"],"backgroundTag":"empty-required-field","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}