{"record":{"id":"9f599ff9d8574648","repo":"SixLabors/ImageSharp","slug":"cannot-identify-image-format-from-empty-data","errorCode":null,"errorMessage":"Cannot identify image format from empty data.","messagePattern":"Cannot identify image format from empty data\\.","errorType":"exception","errorClass":"UnknownImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Image.FromBytes.cs","lineNumber":78,"sourceCode":"        => Identify(DecoderOptions.Default, buffer);\n\n    /// <summary>\n    /// Reads the raw image information from the specified span of bytes without fully decoding it.\n    /// </summary>\n    /// <param name=\"options\">The general decoder options.</param>\n    /// <param name=\"buffer\">The byte span containing encoded image data to read the header from.</param>\n    /// <returns>The <see cref=\"ImageInfo\"/>.</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 ImageInfo Identify(DecoderOptions options, ReadOnlySpan<byte> buffer)\n    {\n        Guard.NotNull(options, nameof(options));\n\n        if (buffer.IsEmpty)\n        {\n            throw new UnknownImageFormatException(\"Cannot identify image format from empty data.\");\n        }\n\n        fixed (byte* ptr = buffer)\n        {\n            using UnmanagedMemoryStream stream = new(ptr, buffer.Length);\n            return Identify(options, stream);\n        }\n    }\n\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=\"buffer\">The byte span containing encoded image data.</param>\n    /// <returns><see cref=\"Image\"/>.</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":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Image.FromBytes.cs#L60-L96","documentation":"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.","triggerScenarios":"Calling Image.Identify(options, ReadOnlySpan<byte>) with a zero-length span, typically from a failed file read or an empty download.","commonSituations":"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.","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."],"exampleFix":"// before\nvar info = Image.Identify(options, buffer);\n// after\nif (buffer.IsEmpty)\n{\n    return null; // no image data available\n}\nvar info = Image.Identify(options, buffer);","handlingStrategy":"validation","validationCode":"if (buffer.Length == 0) return null; // or surface 'no image'","typeGuard":"static bool CanIdentify(ReadOnlySpan<byte> buffer) => buffer.Length > 0;","tryCatchPattern":"try { var info = Image.Identify(options, buffer); }\ncatch (UnknownImageFormatException ex) when (buffer.IsEmpty) { return null; }","preventionTips":["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)."],"tags":["image","empty-input","metadata"],"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"}