LykosAI/StabilityMatrix · error · NotSupportedException

Unknown pixel format

Error message

Unknown pixel format {bitmap.Format}

What it means

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.

Solutions

  1. Convert the bitmap to Rgba8888 or Bgra8888 first (e.g. render it into a new WriteableBitmap/RentRenderTarget with Format=Rgba8888), then call ToSKBitmap.
  2. Ensure images are decoded with a 32-bit format at load time.
  3. Add a pre-check on bitmap.Format and fall back to a re-encoding path instead of the fast copy.

Example fix

// before
var skBitmap = bitmap.ToSKBitmap(); // throws for non-8888 formats

// after
if (bitmap.Format is not (PixelFormat.Rgba8888 or PixelFormat.Bgra8888))
{
    var tmp = new WriteableBitmap(new PixelSize(bitmap.PixelSize.Width, bitmap.PixelSize.Height), new Vector(96, 96), PixelFormat.Rgba8888);
    using (var ctx = tmp.GetGraphicsContext()) ctx.DrawImage(bitmap, new Rect(bitmap.PixelSize.ToSizeWithDpi(new Vector(96,96))));
    bitmap = tmp;
}
var skBitmap = bitmap.ToSKBitmap();
Defensive patterns

Strategy: validation

Validate before calling

if (bitmap.Format is not (PixelFormat.Rgba8888 or PixelFormat.Bgra8888))
    throw new InvalidOperationException("Re-encode bitmap to Rgba8888/Bgra8888 before calling ToSKBitmap.");

Type guard

static bool IsSkConvertible(this Bitmap b) => b.Format is PixelFormat.Rgba8888 or PixelFormat.Bgra8888;

Try / catch

try
{
    var sk = bitmap.ToSKBitmap();
}
catch (NotSupportedException ex)
{
    logger.LogWarning(ex, "Bitmap format {Format} unsupported; re-encoding", bitmap.Format);
    bitmap = ReencodeToRgba8888(bitmap);
    var sk = bitmap.ToSKBitmap();
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/99ccf6bb92a1946d. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Extensions/BitmapExtensions.cs:21

using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using SkiaSharp;

namespace StabilityMatrix.Avalonia.Extensions;

public static class BitmapExtensions
{
    /// <summary>
    /// Converts an Avalonia <see cref="IBitmap"/> to a SkiaSharp <see cref="SKBitmap"/>.
    /// </summary>
    /// <param name="bitmap">The Avalonia bitmap to convert.</param>
    /// <returns>The SkiaSharp bitmap.</returns>
    public static SKBitmap ToSKBitmap(this Bitmap bitmap)
    {
        if (bitmap.Format != PixelFormat.Rgba8888 && bitmap.Format != PixelFormat.Bgra8888)
        {
            throw new NotSupportedException($"Unknown pixel format {bitmap.Format}");
        }

        var skColorType = SKColorType.Bgra8888;
        if (bitmap.Format == PixelFormat.Rgba8888)
        {
            skColorType = SKColorType.Rgba8888;
        }

        var skAlphaType = bitmap.AlphaFormat switch
        {
            AlphaFormat.Premul => SKAlphaType.Premul,
            AlphaFormat.Unpremul => SKAlphaType.Unpremul,
            AlphaFormat.Opaque => SKAlphaType.Opaque,
            _ => SKAlphaType.Premul
        };

        var skBitmap = new SKBitmap(
            bitmap.PixelSize.Width,

View on GitHub (pinned to af93d6ef57)