babalae/better-genshin-impact · error · InvalidDataException

下载内容不是有效的图片。

Error message

下载内容不是有效的图片。

What it means

Thrown by ValidateImage when SixLabors.ImageSharp's Image.Identify returns null — the downloaded file exists and was read, but ImageSharp could not parse any image metadata from it. This is treated as InvalidDataException because the bytes are not a usable image.

Source

Thrown at BetterGenshinImpact/Service/BannerImageService.cs:204

        {
            AllowAutoRedirect = true,
            MaxAutomaticRedirections = 5,
            UseCookies = false,
            UseDefaultCredentials = false
        })
        {
            Timeout = Timeout.InfiniteTimeSpan
        };
        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))
            {

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Verify the URL resolves directly to an image file (open it in a browser and confirm it renders as an image, not a page).
  2. Check the server's Content-Type header and HTTP status — an HTML body with 200 is the usual culprit.
  3. Retry the download; transient corruption on slow links can truncate the header.
  4. If the source is reliable, the file may be a format ImageSharp cannot detect — see error 463.
Defensive patterns

Strategy: validation

Validate before calling

// Inspect Content-Type before streaming to disk.
if (!response.Content.Headers.ContentType?.MediaType?.StartsWith("image/") == true)
{
    throw new InvalidDataException("响应不是图片类型。", );
}

Try / catch

try
{
    ValidateImage(tempPath);
}
catch (InvalidDataException ex) when (ex.Message.Contains("不是有效的图片"))
{
    // The URL did not yield a real image — prompt the user for a valid image URL.
    logger.LogError(ex, "Downloaded content is not a valid image");
}

Prevention

When it happens

Trigger: The downloaded content is HTML (e.g. a 404/error page that still returned HTTP 200), a non-image binary, or a truncated/corrupt image where the header is unreadable but the file is non-empty.

Common situations: URL points to a web page rather than a direct image link; CDN returns an HTML interstitial with status 200; the response was cut short by the MaxDownloadBytes cap mid-header; the server serves a different content type than expected.

Related errors


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