babalae/better-genshin-impact · warning · InvalidDataException

图片像素数量超过 {MaxPixelCount:N0} 限制。

Error message

图片像素数量超过 {MaxPixelCount:N0} 限制。

What it means

Thrown by ValidateImage when the downloaded image's pixel count (width * height) exceeds MaxPixelCount (40,000,000). This is a decompression-bomb guard: it rejects images that would consume excessive memory when decoded, before they can be loaded as a WPF banner.

Source

Thrown at BetterGenshinImpact/Service/BannerImageService.cs:208

            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))
            {
                File.Delete(path);
            }
        }
        catch (IOException)

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Downscale the source image so width * height is under 40,000,000 (e.g. resize to 3840x2160 = ~8.3M pixels).
  2. Use a banner-optimized image with a typical desktop aspect ratio at a reasonable resolution.
  3. If a higher cap is genuinely needed, raise the MaxPixelCount constant — but be aware of the memory cost when WPF decodes it.
Defensive patterns

Strategy: validation

Validate before calling

// After Image.Identify, reject early before any caller tries to decode.
var info = Image.Identify(path) ?? throw new InvalidDataException("下载内容不是有效的图片。");
if ((long)info.Width * info.Height > MaxPixelCount)
{
    throw new InvalidDataException($"图片像素数量超过 {MaxPixelCount:N0} 限制。");
}

Try / catch

try
{
    ValidateImage(tempPath);
}
catch (InvalidDataException ex) when (ex.Message.Contains("像素数量"))
{
    logger.LogWarning("Banner image exceeds pixel limit; ask user to downscale");
}

Prevention

When it happens

Trigger: The downloaded image has very high resolution, e.g. 7000x6000 = 42M pixels. The checked multiplication guards against integer overflow before comparing against the cap.

Common situations: User selects a high-resolution photograph or a stitched panorama as the custom banner; source image is a raw scan or print-ready artwork.

Related errors


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