{"record":{"id":"afb32b62af52678c","repo":"NickeManarin/ScreenToGif","slug":"only-8-24-and-32-bpp-images-are-supported","errorCode":null,"errorMessage":"Only 8, 24 and 32 bpp images are supported.","messagePattern":"Only 8, 24 and 32 bpp images are supported\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"ScreenToGif.Util/Codification/Gif/LegacyEncoder/PixelUtilOld.cs","lineNumber":79,"sourceCode":"    /// </summary>\n    public void LockBits()\n    {\n        // Get width and height of bitmap\n        Width = _source.Width;\n        Height = _source.Height;\n\n        // Get total locked pixels count\n        var pixelCount = Width * Height;\n\n        // Create rectangle to lock\n        var rect = new Rectangle(0, 0, Width, Height);\n\n        // get source bitmap pixel format size\n        Depth = Image.GetPixelFormatSize(_source.PixelFormat);\n\n        // Check if bpp (Bits Per Pixel) is 8, 24, or 32\n        if (Depth != 8 && Depth != 24 && Depth != 32)\n            throw new ArgumentException(\"Only 8, 24 and 32 bpp images are supported.\");\n\n        // Lock bitmap and return bitmap data\n        _bitmapData = _source.LockBits(rect, ImageLockMode.ReadWrite, _source.PixelFormat);\n\n        // Create byte array to copy pixel values\n        var step = Depth / 8;\n        Pixels = new byte[pixelCount * step];\n        Scan0 = _bitmapData.Scan0;\n\n        // Copy data from pointer to array\n        Marshal.Copy(Scan0, Pixels, 0, Pixels.Length);\n    }\n\n    /// <summary>\n    /// Unlock bitmap data\n    /// </summary>\n    public void UnlockBits()\n    {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif.Util/Codification/Gif/LegacyEncoder/PixelUtilOld.cs#L61-L97","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert the source Bitmap to Format32bppRgb or Format24bppRgb using new Bitmap(source, width, height, PixelFormat.Format32bppRgb) before passing it to PixelUtilOld.","Use a Graphics.DrawImage call to clone the bitmap into a supported format.","Switch to the modern PixelUtil (WPF-based) if the codebase supports it, which handles 24/32bpp WriteableBitmaps.","Validate PixelFormat before calling LockBits and convert or reject early."],"exampleFix":"// before\nDepth = Image.GetPixelFormatSize(_source.PixelFormat);\nif (Depth != 8 && Depth != 24 && Depth != 32)\n    throw new ArgumentException(\"Only 8, 24 and 32 bpp images are supported.\");\n\n// after: auto-convert to 32bpp before locking\nif (Depth != 8 && Depth != 24 && Depth != 32)\n{\n    _source = new Bitmap(_source, _source.Width, _source.Height, PixelFormat.Format32bppRgb);\n    Depth = Image.GetPixelFormatSize(_source.PixelFormat);\n}","handlingStrategy":"validation","validationCode":"var depth = Image.GetPixelFormatSize(source.PixelFormat);\nif (depth != 8 && depth != 24 && depth != 32)\n{\n    source = new Bitmap(source, source.Width, source.Height, PixelFormat.Format32bppRgb);\n}","typeGuard":"static bool IsSupportedPixelFormat(Bitmap bmp)\n{\n    var depth = Image.GetPixelFormatSize(bmp.PixelFormat);\n    return depth == 8 || depth == 24 || depth == 32;\n}","tryCatchPattern":"try\n{\n    pixelUtil.LockBits();\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"bpp\"))\n{\n    _source = new Bitmap(_source, _source.Width, _source.Height, PixelFormat.Format32bppRgb);\n    pixelUtil.LockBits(); // retry after conversion\n}","preventionTips":["Normalize all source bitmaps to Format32bppRgb at the pipeline entry point.","Add a pixel-format validation step before entering the GIF encoder.","Use Graphics.DrawImage to clone into a standard format when loading images from disk."],"tags":["gif","image-format","pixelformat","system-drawing","encoder"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}