{"record":{"id":"99ccf6bb92a1946d","repo":"LykosAI/StabilityMatrix","slug":"unknown-pixel-format-bitmap-format","errorCode":null,"errorMessage":"Unknown pixel format {bitmap.Format}","messagePattern":"Unknown pixel format (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Extensions/BitmapExtensions.cs","lineNumber":21,"sourceCode":"using Avalonia;\nusing Avalonia.Media.Imaging;\nusing Avalonia.Platform;\nusing SkiaSharp;\n\nnamespace StabilityMatrix.Avalonia.Extensions;\n\npublic static class BitmapExtensions\n{\n    /// <summary>\n    /// Converts an Avalonia <see cref=\"IBitmap\"/> to a SkiaSharp <see cref=\"SKBitmap\"/>.\n    /// </summary>\n    /// <param name=\"bitmap\">The Avalonia bitmap to convert.</param>\n    /// <returns>The SkiaSharp bitmap.</returns>\n    public static SKBitmap ToSKBitmap(this Bitmap bitmap)\n    {\n        if (bitmap.Format != PixelFormat.Rgba8888 && bitmap.Format != PixelFormat.Bgra8888)\n        {\n            throw new NotSupportedException($\"Unknown pixel format {bitmap.Format}\");\n        }\n\n        var skColorType = SKColorType.Bgra8888;\n        if (bitmap.Format == PixelFormat.Rgba8888)\n        {\n            skColorType = SKColorType.Rgba8888;\n        }\n\n        var skAlphaType = bitmap.AlphaFormat switch\n        {\n            AlphaFormat.Premul => SKAlphaType.Premul,\n            AlphaFormat.Unpremul => SKAlphaType.Unpremul,\n            AlphaFormat.Opaque => SKAlphaType.Opaque,\n            _ => SKAlphaType.Premul\n        };\n\n        var skBitmap = new SKBitmap(\n            bitmap.PixelSize.Width,","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Extensions/BitmapExtensions.cs#L3-L39","documentation":"BitmapExtensions.ToSKBitmap converts an Avalonia Bitmap to an SKBitmap by direct pixel-buffer copy, which only supports the two 32-bit formats Avalonia guarantees here: Rgba8888 and Bgra8888. If bitmap.Format is any other PixelFormat (e.g. Rgb565, single-channel, or null for format-less bitmaps), the method throws NotSupportedException because the byte layout would be misinterpreted.","triggerScenarios":"Calling ToSKBitmap on a Bitmap whose Format is not Rgba8888 or Bgra8888 — e.g. a bitmap loaded/decoded with a different pixel format, a default-constructed or format-less Bitmap (Format is null), or a bitmap created programmatically with a reduced color depth.","commonSituations":"Decoding images from an asset pipeline that produces 16-bit or palette formats; cross-platform differences in decoder output; copying a Bitmap created from a stream that Avalonia decoded into a non-32-bit format.","solutions":["Convert the bitmap to Rgba8888 or Bgra8888 first (e.g. render it into a new WriteableBitmap/RentRenderTarget with Format=Rgba8888), then call ToSKBitmap.","Ensure images are decoded with a 32-bit format at load time.","Add a pre-check on bitmap.Format and fall back to a re-encoding path instead of the fast copy."],"exampleFix":"// before\nvar skBitmap = bitmap.ToSKBitmap(); // throws for non-8888 formats\n\n// after\nif (bitmap.Format is not (PixelFormat.Rgba8888 or PixelFormat.Bgra8888))\n{\n    var tmp = new WriteableBitmap(new PixelSize(bitmap.PixelSize.Width, bitmap.PixelSize.Height), new Vector(96, 96), PixelFormat.Rgba8888);\n    using (var ctx = tmp.GetGraphicsContext()) ctx.DrawImage(bitmap, new Rect(bitmap.PixelSize.ToSizeWithDpi(new Vector(96,96))));\n    bitmap = tmp;\n}\nvar skBitmap = bitmap.ToSKBitmap();","handlingStrategy":"validation","validationCode":"if (bitmap.Format is not (PixelFormat.Rgba8888 or PixelFormat.Bgra8888))\n    throw new InvalidOperationException(\"Re-encode bitmap to Rgba8888/Bgra8888 before calling ToSKBitmap.\");","typeGuard":"static bool IsSkConvertible(this Bitmap b) => b.Format is PixelFormat.Rgba8888 or PixelFormat.Bgra8888;","tryCatchPattern":"try\n{\n    var sk = bitmap.ToSKBitmap();\n}\ncatch (NotSupportedException ex)\n{\n    logger.LogWarning(ex, \"Bitmap format {Format} unsupported; re-encoding\", bitmap.Format);\n    bitmap = ReencodeToRgba8888(bitmap);\n    var sk = bitmap.ToSKBitmap();\n}","preventionTips":["Decode/load bitmaps with 32-bit formats (Rgba8888/Bgra8888)","Check bitmap.Format after every decode, especially cross-platform","Centralize bitmap creation so formats stay uniform"],"tags":["avalonia","skiasharp","bitmap","pixel-format","not-supported"],"backgroundTag":"unsupported-dtype","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}