LykosAI/StabilityMatrix · error · NotSupportedException

Unsupported SKAlphaType

Error message

Unsupported SKAlphaType: {image.AlphaType}

What it means

In the SKImage->WriteableBitmap conversion, ToAvaloniaBitmap maps image.AlphaType to an Avalonia AlphaFormat; only Opaque, Premul, and Unpremul are handled and any other value throws NotSupportedException naming the alpha type. Like error 16, this normally indicates an uninitialized or out-of-range SKAlphaType on the source image.

Solutions

  1. Create the SKImage through supported APIs (Decode/Snapshot) so AlphaType is valid.
  2. Recreate the image with an explicit SKImageInfo { AlphaType = SKAlphaType.Premul } before converting.
  3. Pre-check image.AlphaType with Enum.IsDefined before conversion.

Example fix

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

// after
if (!Enum.IsDefined(typeof(SKAlphaType), image.AlphaType))
    image = image.Rasterize??.Recreate(SKAlphaType.Premul); // or decode anew with SKAlphaType.Premul
var bmp = image.ToAvaloniaBitmap();
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling ToAvaloniaBitmap on an SKImage with an invalid/unknown AlphaType (uninitialized SKImageInfo, raw interop values, or a new enum member from a different SkiaSharp version).

Common situations: Interop with native Skia handles carrying out-of-range alpha values; hand-rolled SKImageInfo structs; SkiaSharp version drift after dependency upgrades.

Related errors


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

Appendix: source

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

            using var bitmap = SKBitmap.FromImage(image);
            return bitmap.ToAvaloniaBitmap(dpi);
        }

        // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault
        var avaloniaColorFormat = image.ColorType switch
        {
            SKColorType.Rgba8888 => PixelFormat.Rgba8888,
            SKColorType.Bgra8888 => PixelFormat.Bgra8888,
            _ => throw new NotSupportedException($"Unsupported SKColorType: {image.ColorType}"),
        };

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

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

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

View on GitHub (pinned to af93d6ef57)