NickeManarin/ScreenToGif · error · ArgumentException

Only 8, 24 and 32 bpp images are supported.

Error message

Only 8, 24 and 32 bpp images are supported.

What it means

Thrown by the legacy GIF encoder's PixelUtilOld.LockBits when the source System.Drawing.Bitmap has a pixel format other than 8, 24, or 32 bits per pixel. The encoder manually locks and reads pixel data, and its byte-level logic only handles those three depths.

Source

Thrown at ScreenToGif.Util/Codification/Gif/LegacyEncoder/PixelUtilOld.cs:79

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

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

        // Create rectangle to lock
        var rect = new Rectangle(0, 0, Width, Height);

        // get source bitmap pixel format size
        Depth = Image.GetPixelFormatSize(_source.PixelFormat);

        // Check if bpp (Bits Per Pixel) is 8, 24, or 32
        if (Depth != 8 && Depth != 24 && Depth != 32)
            throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");

        // Lock bitmap and return bitmap data
        _bitmapData = _source.LockBits(rect, ImageLockMode.ReadWrite, _source.PixelFormat);

        // Create byte array to copy pixel values
        var step = Depth / 8;
        Pixels = new byte[pixelCount * step];
        Scan0 = _bitmapData.Scan0;

        // Copy data from pointer to array
        Marshal.Copy(Scan0, Pixels, 0, Pixels.Length);
    }

    /// <summary>
    /// Unlock bitmap data
    /// </summary>
    public void UnlockBits()
    {

View on GitHub (pinned to a4d0a67c21)

Solutions

  1. Convert the source Bitmap to Format32bppRgb or Format24bppRgb using new Bitmap(source, width, height, PixelFormat.Format32bppRgb) before passing it to PixelUtilOld.
  2. Use a Graphics.DrawImage call to clone the bitmap into a supported format.
  3. Switch to the modern PixelUtil (WPF-based) if the codebase supports it, which handles 24/32bpp WriteableBitmaps.
  4. Validate PixelFormat before calling LockBits and convert or reject early.

Example fix

// before
Depth = Image.GetPixelFormatSize(_source.PixelFormat);
if (Depth != 8 && Depth != 24 && Depth != 32)
    throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");

// after: auto-convert to 32bpp before locking
if (Depth != 8 && Depth != 24 && Depth != 32)
{
    _source = new Bitmap(_source, _source.Width, _source.Height, PixelFormat.Format32bppRgb);
    Depth = Image.GetPixelFormatSize(_source.PixelFormat);
}
Defensive patterns

Strategy: validation

Validate before calling

var depth = Image.GetPixelFormatSize(source.PixelFormat);
if (depth != 8 && depth != 24 && depth != 32)
{
    source = new Bitmap(source, source.Width, source.Height, PixelFormat.Format32bppRgb);
}

Type guard

static bool IsSupportedPixelFormat(Bitmap bmp)
{
    var depth = Image.GetPixelFormatSize(bmp.PixelFormat);
    return depth == 8 || depth == 24 || depth == 32;
}

Try / catch

try
{
    pixelUtil.LockBits();
}
catch (ArgumentException ex) when (ex.Message.Contains("bpp"))
{
    _source = new Bitmap(_source, _source.Width, _source.Height, PixelFormat.Format32bppRgb);
    pixelUtil.LockBits(); // retry after conversion
}

Prevention

When it happens

Trigger: LockBits is called on a Bitmap whose PixelFormat is 1bpp, 4bpp, 16bpp, 48bpp, or 64bpp. Common when the source image was created with a non-standard format or loaded from a format (like certain TIFFs) that preserves high bit depths.

Common situations: User imports a 16-bit or 48-bit image (common with HDR or scientific imaging). Source bitmap was created programmatically with Format16bppRgb555 or similar. Image was loaded from a TIFF that retains its original depth instead of being converted.

Related errors


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