{"record":{"id":"a6a14a095d46d185","repo":"NickeManarin/ScreenToGif","slug":"only-24-and-32-bpp-images-are-supported","errorCode":null,"errorMessage":"Only 24 and 32 bpp images are supported.","messagePattern":"Only 24 and 32 bpp images are supported\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"ScreenToGif.Util/Codification/PixelUtil.cs","lineNumber":74,"sourceCode":"\n    /// <summary>\n    /// Lock bitmap data.\n    /// </summary>\n    public void LockBits()\n    {\n        //Get width and height of bitmap.\n        Width = _source.PixelWidth;\n        Height = _source.PixelHeight;\n\n        //Get total locked pixels count.\n        var pixelCount = Width * Height;\n\n        //Get source bitmap pixel format size.\n        Depth = _source.Format.BitsPerPixel;\n        ChannelsPerPixel = Depth / 8;\n\n        if (Depth != 32 && Depth != 24)\n            throw new ArgumentException(\"Only 24 and 32 bpp images are supported.\");\n\n        _data = new WriteableBitmap(_source);\n\n        //Lock bitmap and return bitmap data.\n        _data.Lock();\n\n        /*\n            https://doanvublog.wordpress.com/tag/32bpp/\n            1,4,8 and 16bpp uses a color table.\n\n            1bpp : 1 byte, 8 pixels, 2 colors\n            4bpp : 1 byte, 2 pixels, 16 colors\n            8bpp : 1 byte, 1 pixel, 256 colors\n            16bpp : 2 bytes, 1 pixel\n            24bpp : 3 bytes, 1 pixel\n            32bpp : 4 bytes, 1 pixel\n\n            So, bpp/8 = color chunk size.","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif.Util/Codification/PixelUtil.cs#L56-L92","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Convert the BitmapSource to Bgr32 or Bgra32 using new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0) before creating PixelUtil.","Use WriteableBitmap.CopyPixels with a Format conversion step.","Validate source.Format.BitsPerPixel before constructing PixelUtil and convert or reject.","Ensure screen capture code creates bitmaps in a supported format from the start."],"exampleFix":"// before\nDepth = _source.Format.BitsPerPixel;\nif (Depth != 32 && Depth != 24)\n    throw new ArgumentException(\"Only 24 and 32 bpp images are supported.\");\n\n// after: convert unsupported formats to Bgra32\nif (Depth != 32 && Depth != 24)\n{\n    var converter = new FormatConvertedBitmap(_source, PixelFormats.Bgra32, null, 0);\n    _source = BitmapFrame.Create(converter);\n    Depth = 32;\n}","handlingStrategy":"validation","validationCode":"if (source.Format.BitsPerPixel != 24 && source.Format.BitsPerPixel != 32)\n{\n    source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);\n    source.Freeze();\n}","typeGuard":"static bool IsSupportedWpfFormat(BitmapSource src)\n{\n    return src.Format.BitsPerPixel == 24 || src.Format.BitsPerPixel == 32;\n}","tryCatchPattern":"try\n{\n    pixelUtil.LockBitsAndPad();\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"bpp\"))\n{\n    _source = (BitmapSource)new FormatConvertedBitmap(_source, PixelFormats.Bgra32, null, 0);\n    pixelUtil.LockBitsAndPad();\n}","preventionTips":["Convert all BitmapSource inputs to Bgra32 at the capture/import boundary.","Ensure screen capture code creates bitmaps in Bgra32 from the start.","Add a Format validation assertion at the top of the encoding pipeline."],"tags":["wpf","image-format","pixelformat","bitmapsource","encoder"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}