NickeManarin/ScreenToGif · error · ArgumentException

Only 24 and 32 bpp images are supported.

Error message

Only 24 and 32 bpp images are supported.

What it means

Thrown by PixelUtil.LockBitsAndPad (WPF-based pixel utility) when the source BitmapSource has a Format with BitsPerPixel other than 24 or 32. This method reads raw pixel data via WriteableBitmap and only supports Bgr24/Bgra32/Rgb24/Rgba32 equivalent formats.

Source

Thrown at ScreenToGif.Util/Codification/PixelUtil.cs:74

    /// <summary>
    /// Lock bitmap data.
    /// </summary>
    public void LockBits()
    {
        //Get width and height of bitmap.
        Width = _source.PixelWidth;
        Height = _source.PixelHeight;

        //Get total locked pixels count.
        var pixelCount = Width * Height;

        //Get source bitmap pixel format size.
        Depth = _source.Format.BitsPerPixel;
        ChannelsPerPixel = Depth / 8;

        if (Depth != 32 && Depth != 24)
            throw new ArgumentException("Only 24 and 32 bpp images are supported.");

        _data = new WriteableBitmap(_source);

        //Lock bitmap and return bitmap data.
        _data.Lock();

        /*
            https://doanvublog.wordpress.com/tag/32bpp/
            1,4,8 and 16bpp uses a color table.

            1bpp : 1 byte, 8 pixels, 2 colors
            4bpp : 1 byte, 2 pixels, 16 colors
            8bpp : 1 byte, 1 pixel, 256 colors
            16bpp : 2 bytes, 1 pixel
            24bpp : 3 bytes, 1 pixel
            32bpp : 4 bytes, 1 pixel

            So, bpp/8 = color chunk size.

View on GitHub (pinned to a4d0a67c21)

Solutions

  1. Convert the BitmapSource to Bgr32 or Bgra32 using new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0) before creating PixelUtil.
  2. Use WriteableBitmap.CopyPixels with a Format conversion step.
  3. Validate source.Format.BitsPerPixel before constructing PixelUtil and convert or reject.
  4. Ensure screen capture code creates bitmaps in a supported format from the start.

Example fix

// before
Depth = _source.Format.BitsPerPixel;
if (Depth != 32 && Depth != 24)
    throw new ArgumentException("Only 24 and 32 bpp images are supported.");

// after: convert unsupported formats to Bgra32
if (Depth != 32 && Depth != 24)
{
    var converter = new FormatConvertedBitmap(_source, PixelFormats.Bgra32, null, 0);
    _source = BitmapFrame.Create(converter);
    Depth = 32;
}
Defensive patterns

Strategy: validation

Validate before calling

if (source.Format.BitsPerPixel != 24 && source.Format.BitsPerPixel != 32)
{
    source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
    source.Freeze();
}

Type guard

static bool IsSupportedWpfFormat(BitmapSource src)
{
    return src.Format.BitsPerPixel == 24 || src.Format.BitsPerPixel == 32;
}

Try / catch

try
{
    pixelUtil.LockBitsAndPad();
}
catch (ArgumentException ex) when (ex.Message.Contains("bpp"))
{
    _source = (BitmapSource)new FormatConvertedBitmap(_source, PixelFormats.Bgra32, null, 0);
    pixelUtil.LockBitsAndPad();
}

Prevention

When it happens

Trigger: LockBitsAndPad is called on a BitmapSource whose Format.BitsPerPixel is not 24 or 32 (e.g., Bgr555 = 16bpp, Gray8 = 8bpp, Rgba64 = 64bpp, Bgr101010 = 32bpp but different channel layout).

Common situations: Screen capture produced a 16-bit or indexed-color bitmap. User imported an image with an exotic pixel format. A WPF rendering operation returned a non-standard format bitmap. Animated cursor or icon converted to BitmapSource with low bit depth.

Related errors


AI-assisted analysis of NickeManarin/ScreenToGif@a4d0a67c21 (2026-08-13). Data as JSON: /api/errors/a6a14a095d46d185. Report an issue: GitHub.