babalae/better-genshin-impact · error · InvalidDataException

下载内容不是支持的图片格式。

Error message

下载内容不是支持的图片格式。

What it means

Thrown when SixLabors.ImageSharp raises UnknownImageFormatException during Image.Identify — the file's magic bytes do not match any image format decoder that ImageSharp has registered. The service wraps it as InvalidDataException with a clearer message.

Source

Thrown at BetterGenshinImpact/Service/BannerImageService.cs:213

        client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("BetterGI-Banner", "1.0"));
        return client;
    }

    private static void ValidateImage(string path)
    {
        try
        {
            var imageInfo = Image.Identify(path)
                            ?? throw new InvalidDataException("下载内容不是有效的图片。");
            var pixelCount = checked((long)imageInfo.Width * imageInfo.Height);
            if (pixelCount > MaxPixelCount)
            {
                throw new InvalidDataException($"图片像素数量超过 {MaxPixelCount:N0} 限制。");
            }
        }
        catch (UnknownImageFormatException ex)
        {
            throw new InvalidDataException("下载内容不是支持的图片格式。", ex);
        }
    }

    private static void TryDeleteFile(string path)
    {
        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
        catch (IOException)
        {
            // 临时文件清理失败不应覆盖原始下载异常。
        }
        catch (UnauthorizedAccessException)
        {

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Use a universally supported format: JPEG, PNG, BMP, or GIF.
  2. If WebP support is required, ensure the SixLabors.ImageSharp.Webp NuGet package is referenced and registered in the ImageSharp Configuration.
  3. Re-encode the source image to JPEG/PNG before hosting it.
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    ValidateImage(tempPath);
}
catch (InvalidDataException ex) when (ex.InnerException is UnknownImageFormatException)
{
    // Format not supported — ask the user for a JPEG/PNG/BMP/GIF.
    logger.LogError(ex, "Unsupported image format");
}

Prevention

When it happens

Trigger: The downloaded content is an image format ImageSharp does not support by default (e.g. WebP without the SixLabors.ImageSharp.Webp package, AVIF, HEIC, SVG, ICO), or the file is mislabeled (extension says .jpg but bytes are something else).

Common situations: User picks a modern format URL (WebP/AVIF) that the bundled ImageSharp configuration cannot decode; URL serves SVG; a server-side format converter is misconfigured.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/00d3b0d2623fae46. Report an issue: GitHub.