{"record":{"id":"654fe103c75c8f15","repo":"LykosAI/StabilityMatrix","slug":"unsupported-skalphatype-bitmap-alphatype","errorCode":null,"errorMessage":"Unsupported SKAlphaType: {bitmap.AlphaType}","messagePattern":"Unsupported SKAlphaType: (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Extensions/SkiaExtensions.cs","lineNumber":176,"sourceCode":"    }\n\n    public static Bitmap ToAvaloniaBitmap(this SKBitmap bitmap, Vector dpi)\n    {\n        // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault\n        var avaloniaColorFormat = bitmap.ColorType switch\n        {\n            SKColorType.Rgba8888 => PixelFormat.Rgba8888,\n            SKColorType.Bgra8888 => PixelFormat.Bgra8888,\n            _ => throw new NotSupportedException($\"Unsupported SKColorType: {bitmap.ColorType}\"),\n        };\n\n        // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault\n        var avaloniaAlphaFormat = bitmap.AlphaType switch\n        {\n            SKAlphaType.Opaque => AlphaFormat.Opaque,\n            SKAlphaType.Premul => AlphaFormat.Premul,\n            SKAlphaType.Unpremul => AlphaFormat.Unpremul,\n            _ => throw new NotSupportedException($\"Unsupported SKAlphaType: {bitmap.AlphaType}\"),\n        };\n\n        var result = new WriteableBitmap(\n            new PixelSize(bitmap.Width, bitmap.Height),\n            dpi,\n            avaloniaColorFormat,\n            avaloniaAlphaFormat\n        );\n\n        using var framebuffer = result.Lock();\n        CopyPixelRows(\n            bitmap.GetPixels(),\n            bitmap.RowBytes,\n            framebuffer.Address,\n            framebuffer.RowBytes,\n            bitmap.Height\n        );\n        return result;","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Extensions/SkiaExtensions.cs#L158-L194","documentation":"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.)","triggerScenarios":"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.","commonSituations":"Manually constructed SKImageInfo with a bogus AlphaType; interop with native code writing raw enum values; mismatched SkiaSharp native library versions after an upgrade.","solutions":["Ensure the SKBitmap was created via SKBitmap.Decode/normal APIs so AlphaType is a valid value.","Force a known alpha type by copying: skBitmap.Copy(SKColorType.Bgra8888) or recreate with SKImageInfo { AlphaType = SKAlphaType.Premul }.","Add a pre-check on bitmap.AlphaType before converting."],"exampleFix":"// before\nvar bmp = skBitmap.ToAvaloniaBitmap();\n\n// after\nif (!Enum.IsDefined(typeof(SKAlphaType), skBitmap.AlphaType))\n    skBitmap = new SKBitmap(new SKImageInfo(skBitmap.Width, skBitmap.Height, SKColorType.Bgra8888, SKAlphaType.Premul));\nvar bmp = skBitmap.ToAvaloniaBitmap();","handlingStrategy":"type-guard","validationCode":"if (!Enum.IsDefined(typeof(SKAlphaType), bitmap.AlphaType))\n    throw new InvalidOperationException(\"Invalid SKAlphaType on source bitmap.\");","typeGuard":"static bool HasValidAlpha(this SKBitmap b) => b.AlphaType is SKAlphaType.Opaque or SKAlphaType.Premul or SKAlphaType.Unpremul;","tryCatchPattern":"try\n{\n    return bitmap.ToAvaloniaBitmap();\n}\ncatch (NotSupportedException ex)\n{\n    logger.LogWarning(ex, \"Bad alpha type; re-creating bitmap with Premul\");\n    return RecreateWithPremul(bitmap).ToAvaloniaBitmap();\n}","preventionTips":["Create SKBitmaps via Decode or well-formed SKImageInfo","Avoid raw enum casts from native interop","Pin/align SkiaSharp versions across packages"],"tags":["skiasharp","avalonia","alpha-type","not-supported"],"backgroundTag":"unsupported-enum-value","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"}