{"record":{"id":"bdbe13c0c82b7a50","repo":"SixLabors/ImageSharp","slug":"cannot-detect-image-format-from-empty-data","errorCode":null,"errorMessage":"Cannot detect image format from empty data.","messagePattern":"Cannot detect image format from empty data\\.","errorType":"exception","errorClass":"UnknownImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Image.FromBytes.cs","lineNumber":41,"sourceCode":"        => DetectFormat(DecoderOptions.Default, buffer);\n\n    /// <summary>\n    /// By reading the header on the provided byte span this calculates the images format.\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=\"IImageFormat\"/>.</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 IImageFormat DetectFormat(DecoderOptions options, ReadOnlySpan<byte> buffer)\n    {\n        Guard.NotNull(options, nameof(options));\n\n        if (buffer.IsEmpty)\n        {\n            throw new UnknownImageFormatException(\"Cannot detect image format from empty data.\");\n        }\n\n        fixed (byte* ptr = buffer)\n        {\n            using UnmanagedMemoryStream stream = new(ptr, buffer.Length);\n            return DetectFormat(options, stream);\n        }\n    }\n\n    /// <summary>\n    /// Reads the raw image information from the specified stream without fully decoding it.\n    /// </summary>\n    /// <param name=\"buffer\">The byte array containing encoded image data to read the header from.</param>\n    /// <returns>The <see cref=\"ImageInfo\"/>.</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>\n    public static ImageInfo Identify(ReadOnlySpan<byte> buffer)","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Image.FromBytes.cs#L23-L59","documentation":"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.","triggerScenarios":"Calling Image.DetectFormat(options, ReadOnlySpan<byte>) with a span of length 0, e.g. after reading zero bytes from a file or network stream.","commonSituations":"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.","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."],"exampleFix":"// before\nvar format = Image.DetectFormat(options, buffer);\n// after\nif (buffer.IsEmpty)\n{\n    throw new InvalidDataException(\"No image data was provided.\");\n}\nvar format = Image.DetectFormat(options, buffer);","handlingStrategy":"validation","validationCode":"if (buffer.IsEmpty) throw new ArgumentException(\"Image data must not be empty.\", nameof(buffer));","typeGuard":"static bool HasImageData(ReadOnlySpan<byte> buffer) => !buffer.IsEmpty;","tryCatchPattern":"try { var format = Image.DetectFormat(options, buffer); }\ncatch (UnknownImageFormatException ex) when (buffer.IsEmpty) { /* treat as no image */ }","preventionTips":["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."],"tags":["image","empty-input","argument-validation"],"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"}