LykosAI/StabilityMatrix · error · NotSupportedException

Unsupported SKAlphaType

Error message

Unsupported SKAlphaType: {bitmap.AlphaType}

What it means

In the same SKBitmap->WriteableBitmap conversion, after mapping the color type, ToAvaloniaBitmap maps SKAlphaType to Avalonia AlphaFormat. Only Opaque, Premul, and Unpremul are handled; unknown alpha types hit the default arm and throw NotSupportedException naming the alpha type. (In practice almost every SKAlphaType value is covered, so this usually indicates an uninitialized or out-of-range value.)

Solutions

  1. Ensure the SKBitmap was created via SKBitmap.Decode/normal APIs so AlphaType is a valid value.
  2. Force a known alpha type by copying: skBitmap.Copy(SKColorType.Bgra8888) or recreate with SKImageInfo { AlphaType = SKAlphaType.Premul }.
  3. Add a pre-check on bitmap.AlphaType before converting.

Example fix

// before
var bmp = skBitmap.ToAvaloniaBitmap();

// after
if (!Enum.IsDefined(typeof(SKAlphaType), skBitmap.AlphaType))
    skBitmap = new SKBitmap(new SKImageInfo(skBitmap.Width, skBitmap.Height, SKColorType.Bgra8888, SKAlphaType.Premul));
var bmp = skBitmap.ToAvaloniaBitmap();
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Enum.IsDefined(typeof(SKAlphaType), bitmap.AlphaType))
    throw new InvalidOperationException("Invalid SKAlphaType on source bitmap.");

Type guard

static bool HasValidAlpha(this SKBitmap b) => b.AlphaType is SKAlphaType.Opaque or SKAlphaType.Premul or SKAlphaType.Unpremul;

Try / catch

try
{
    return bitmap.ToAvaloniaBitmap();
}
catch (NotSupportedException ex)
{
    logger.LogWarning(ex, "Bad alpha type; re-creating bitmap with Premul");
    return RecreateWithPremul(bitmap).ToAvaloniaBitmap();
}

Prevention

When it happens

Trigger: Calling ToAvaloniaBitmap on an SKBitmap whose AlphaType is uninitialized, cast from an invalid int, or from a future/foreign SkiaSharp version enum value outside the three handled cases.

Common situations: Manually constructed SKImageInfo with a bogus AlphaType; interop with native code writing raw enum values; mismatched SkiaSharp native library versions after an upgrade.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Avalonia/Extensions/SkiaExtensions.cs:176

    }

    public static Bitmap ToAvaloniaBitmap(this SKBitmap bitmap, Vector dpi)
    {
        // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault
        var avaloniaColorFormat = bitmap.ColorType switch
        {
            SKColorType.Rgba8888 => PixelFormat.Rgba8888,
            SKColorType.Bgra8888 => PixelFormat.Bgra8888,
            _ => throw new NotSupportedException($"Unsupported SKColorType: {bitmap.ColorType}"),
        };

        // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault
        var avaloniaAlphaFormat = bitmap.AlphaType switch
        {
            SKAlphaType.Opaque => AlphaFormat.Opaque,
            SKAlphaType.Premul => AlphaFormat.Premul,
            SKAlphaType.Unpremul => AlphaFormat.Unpremul,
            _ => throw new NotSupportedException($"Unsupported SKAlphaType: {bitmap.AlphaType}"),
        };

        var result = new WriteableBitmap(
            new PixelSize(bitmap.Width, bitmap.Height),
            dpi,
            avaloniaColorFormat,
            avaloniaAlphaFormat
        );

        using var framebuffer = result.Lock();
        CopyPixelRows(
            bitmap.GetPixels(),
            bitmap.RowBytes,
            framebuffer.Address,
            framebuffer.RowBytes,
            bitmap.Height
        );
        return result;

View on GitHub (pinned to af93d6ef57)