{"record":{"id":"7db064115d536e74","repo":"QL-Win/QuickLook","slug":"unsupported-pixelformat-pixelformat","errorCode":null,"errorMessage":"Unsupported PixelFormat: {pixelFormat}","messagePattern":"Unsupported PixelFormat: (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"QuickLook.Plugin/QuickLook.Plugin.ImageViewer/AnimatedImage/Providers/CursorProvider.cs","lineNumber":299,"sourceCode":"        Bitmap?.Dispose();\n    }\n}\n\nfile static class Extension\n{\n    public static WriteableBitmap ToWriteableBitmap(this Bitmap bitmap)\n    {\n        if (bitmap == null) throw new ArgumentNullException(nameof(bitmap));\n\n        var pixelFormat = bitmap.PixelFormat;\n        var width = bitmap.Width;\n        var height = bitmap.Height;\n\n        var wpfPixelFormat = pixelFormat switch\n        {\n            PixelFormat.Format32bppArgb => PixelFormats.Bgra32,\n            PixelFormat.Format24bppRgb => PixelFormats.Bgr24,\n            _ => throw new NotSupportedException($\"Unsupported PixelFormat: {pixelFormat}\")\n        };\n\n        var writeableBitmap = new WriteableBitmap(width, height, 96, 96, wpfPixelFormat, null);\n\n        var bitmapData = bitmap.LockBits(\n            new Rectangle(0, 0, width, height),\n            ImageLockMode.ReadOnly,\n            pixelFormat);\n\n        try\n        {\n            writeableBitmap.Lock();\n            unsafe\n            {\n                Buffer.MemoryCopy(\n                    source: bitmapData.Scan0.ToPointer(),\n                    destination: writeableBitmap.BackBuffer.ToPointer(),\n                    destinationSizeInBytes: writeableBitmap.BackBufferStride * height,","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/QL-Win/QuickLook/blob/cb5d9c429c81d9796fac469da2a68efb5626946d/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/AnimatedImage/Providers/CursorProvider.cs#L281-L317","documentation":"Thrown by CursorProvider.ToWriteableBitmap when a System.Drawing.Bitmap's PixelFormat is neither Format32bppArgb nor Format24bppRgb. The extension only knows how to copy those two layouts into a WPF WriteableBitmap, so any other format (16bpp, 8bpp indexed, 48bpp, etc.) is rejected with NotSupportedException.","triggerScenarios":"bitmap.PixelFormat returns a value outside {Format32bppArgb, Format24bppRgb}; the switch expression hits its '_' arm. Happens when a .cur/.ani decoder (or a resize/conversion step) yields an indexed or 16-bit bitmap.","commonSituations":"An old 256-color or 16-color cursor; a cursor decoder that returns Format8bppIndexed; a palette-based .ani frame; a bitmap that passed through a thumbnail/resize API that downgraded color depth.","solutions":["Before calling ToWriteableBitmap, clone the bitmap into Format32bppArgb: Bitmap.Clone(rect, PixelFormat.Format32bppArgb).","Use Graphics.DrawImage to paint the source onto a new 32bppArgb canvas of the same dimensions.","Extend the switch to handle the missing format (e.g. Format8bppIndexed -> PaletteMapping) if you control the provider.","Validate bitmap.PixelFormat up front and skip/replace the frame if unsupported."],"exampleFix":"// before\nvar wpfPixelFormat = pixelFormat switch\n{\n    PixelFormat.Format32bppArgb => PixelFormats.Bgra32,\n    PixelFormat.Format24bppRgb => PixelFormats.Bgr24,\n    _ => throw new NotSupportedException(...)\n};\n\n// after — normalize to 32bppArgb first\nif (pixelFormat != PixelFormat.Format32bppArgb && pixelFormat != PixelFormat.Format24bppRgb)\n    bitmap = bitmap.Clone(new Rectangle(0,0,bitmap.Width,bitmap.Height), PixelFormat.Format32bppArgb);","handlingStrategy":"validation","validationCode":"var pf = bitmap.PixelFormat;\nif (pf != System.Drawing.Imaging.PixelFormat.Format32bppArgb\n && pf != System.Drawing.Imaging.PixelFormat.Format24bppRgb)\n    bitmap = bitmap.Clone(new Rectangle(0,0,bitmap.Width,bitmap.Height),\n                          System.Drawing.Imaging.PixelFormat.Format32bppArgb);","typeGuard":"static bool IsSupportedCursorFormat(System.Drawing.Bitmap b) => b.PixelFormat is System.Drawing.Imaging.PixelFormat.Format32bppArgb or System.Drawing.Imaging.PixelFormat.Format24bppRgb;","tryCatchPattern":"try { wb = bitmap.ToWriteableBitmap(); }\ncatch (NotSupportedException) { wb = bitmap.Clone(rect, Format32bppArgb).ToWriteableBitmap(); }","preventionTips":["Normalize cursor/ani frames to 32bppArgb before conversion.","Avoid indexed/palette bitmaps for WPF interop.","Extend the switch when a new format must be supported."],"tags":["cursor","image","gdiplus","wpf","pixel-format"],"backgroundTag":null,"analyzedSha":"cb5d9c429c81d9796fac469da2a68efb5626946d","analyzedAt":"2026-08-13T11:51:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}