mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.17k stars 522 forks source link

System.DllNotFoundException: libSkiaSharp exception is thrown in Blazor WASM .NET 8.0 #2745

Open Akash26Arul opened 3 months ago

Akash26Arul commented 3 months ago

Description

System.DllNotFoundException: libSkiaSharp exception is thrown while using SkiaSharp library in Blazor WASM .NET 8.0 and this issue doesn't occur in Blazor WASM .NET 6.0.

Stack trace:

System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception. ---> System.DllNotFoundException: libSkiaSharp at SkiaSharp.SKImageInfo..cctor()

Note: When trying the same approach in Blazor WASM .NET 6.0, there is no issues.

Code

try 
{ 
    using (SKBitmap bitmap = new SKBitmap(500, 500, SKImageInfo.PlatformColorType, SKAlphaType.Premul)) 
    { 
        using (SKSurface sKSurface = SKSurface.Create(bitmap.Info)) 
        { 
            SKCanvas sKCanvas = sKSurface.Canvas; 
            SKPaint sKPaint = new SKPaint(); 
            sKPaint.Color = new SKColor(255, 0, 0); 
            sKCanvas.DrawText("Blazor", new SKPoint(250, 250), sKPaint); 
            MemoryStream stream = new MemoryStream(); 
            sKSurface.Snapshot().Encode(SKEncodedImageFormat.Png, 100).SaveTo(stream); 
        } 
    } 
} 
catch (Exception ex) 
{ 
    ex.ToString(); 
} 

Expected Behavior

Should draw the text and save as png.

Actual Behavior

Throws the above mentioned exception (System.DllNotFoundException: libSkiaSharp)

Version of SkiaSharp

2.88.6

Last Known Good Version of SkiaSharp

None

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Windows

Platform / Operating System Version

.NET 8.0

ASP.NET Core .NET 8.0 : Working fine

Blazor server side .NET 8.0 : Working fine

Blazor client side (WASM) .NET 8.0 : Facing Lib SkiaSharp issue

Akash26Arul commented 3 months ago

Hi team,

Any update on this?

Regards, Akash.

torourke55 commented 3 months ago

I was able to run your code after adding SkiaSharp.Views.Blazor package. https://www.nuget.org/packages/SkiaSharp.Views.Blazor/2.88.7. I'm using WASM .NET 8.0 Visual Studio 2022 version 17.9.1.