d2phap/ImageGlass · error · ArgumentException

Bitmap contains no data.

Error message

Bitmap contains no data.

What it means

Thrown by WebPWrapper.EncodeLossy when bmp.Width == 0 || bmp.Height == 0. The encoder needs a non-zero source bitmap; a zero-dimension bitmap carries no pixels and is rejected up front as ArgumentException (parameter 'bmp'). This fires before the dimension/pixel-format checks, so it is the first validation gate on the encode path.

Source

Thrown at v9/Components/ImageGlass.WebP/WebPWrapper.cs:385

    /// <param name="quality">Between 0 (lower quality, lowest file size) and 100 (highest quality, higher file size)</param>
    public void Save(Bitmap bmp, string pathFileName, int quality = 75)
    {
        // Encode in webP format
        var rawWebP = EncodeLossy(bmp, quality);

        // Write webP file
        File.WriteAllBytes(pathFileName, rawWebP);
    }

    /// <summary>Lossy encoding bitmap to WebP (Simple encoding API)</summary>
    /// <param name="bmp">Bitmap with the image</param>
    /// <param name="quality">Between 0 (lower quality, lowest file size) and 100 (highest quality, higher file size)</param>
    /// <returns>Compressed data</returns>
    public byte[] EncodeLossy(Bitmap bmp, int quality = 75)
    {
        // test bmp
        if (bmp.Width == 0 || bmp.Height == 0)
            throw new ArgumentException("Bitmap contains no data.", nameof(bmp));

        if (bmp.Width > WEBP_MAX_DIMENSION || bmp.Height > WEBP_MAX_DIMENSION)
            throw new NotSupportedException($"Bitmap's dimension is too large. Max is {WEBP_MAX_DIMENSION}x{WEBP_MAX_DIMENSION} pixels.");

        if (bmp.PixelFormat != PixelFormat.Format24bppRgb && bmp.PixelFormat != PixelFormat.Format32bppArgb)
            throw new NotSupportedException("Only support Format24bppRgb and Format32bppArgb pixelFormat.");

        BitmapData? bmpData = null;
        var unmanagedData = IntPtr.Zero;

        try
        {
            int size;

            // Get bmp data
            bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);

            // Compress the bmp data

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Validate bmp != null && bmp.Width > 0 && bmp.Height > 0 before calling EncodeLossy.
  2. Clamp upstream resize/crop outputs to a minimum of 1x1 (or skip encoding when the result would be empty).
  3. Guard the source-load path so a 0x0 decode is reported as an error earlier, not handed to the encoder.
  4. Catch ArgumentException at the call site and surface 'source image is empty' to the user.

Example fix

// before
var raw = new WebPWrapper().EncodeLossy(bmp, quality);

// after: precondition check
if (bmp is null || bmp.Width == 0 || bmp.Height == 0)
    throw new ArgumentException("Source bitmap must have non-zero dimensions.", nameof(bmp));
var raw = new WebPWrapper().EncodeLossy(bmp, quality);
Defensive patterns

Strategy: validation

Validate before calling

if (bmp is null || bmp.Width == 0 || bmp.Height == 0)
    throw new ArgumentException("Source bitmap must have non-zero dimensions.", nameof(bmp));

Type guard

static bool IsEncodable(Bitmap bmp) => bmp is not null && bmp.Width > 0 && bmp.Height > 0;

Try / catch

try { raw = new WebPWrapper().EncodeLossy(bmp, quality); }
catch (ArgumentException ex) when (ex.ParamName == nameof(bmp))
{ /* source bitmap is empty (0x0) */ }

Prevention

When it happens

Trigger: Passing a freshly-allocated 'new Bitmap(0, 0)' or 'new Bitmap(w, 0)'; a bitmap whose dimensions were set to zero by an upstream crop/resize that clamped to 0; a placeholder/uninitialized bitmap handed to the encoder.

Common situations: An image-resize/crop computes a zero target dimension under edge inputs (selection smaller than 1px); loading a corrupt source produced a 0x0 bitmap; default-constructed bitmap passed by mistake.

Related errors


AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13). Data as JSON: /api/errors/1d81581c7bda808a. Report an issue: GitHub.