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.42k stars 535 forks source link

Trying ASP.NET Core Blazor WebAssembly with Skiasharp #1867

Open peder1896 opened 2 years ago

peder1896 commented 2 years ago

Hi I tried to use skiasharp views in Blazor Wasm on Visual Studio 2022 production . Installed SkiaSharp.Views.Blazor 2.88.0-preview.155 added

 SKCanvasView OnPaintSurface="@OnPaintSurface" 

and

code {
    private void OnPaintSurface(SKPaintSurfaceEventArgs e)
    {
        var canvas = e.Surface.Canvas;
       canvas.Clear(SKColors.White);
       using var paint = new SKPaint
        {
            Color = SKColors.Black,
            IsAntialias = true,
            TextSize = 24
        };
       canvas.DrawText("SkiaSharp", 0, 24, paint);
    }
}

to razor file Compiled the projeect ran it but get the following error

Unhandled Exception:

System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception. ---> System.DllNotFoundException: libSkiaSharp at SkiaSharp.SKImageInfo..cctor() --- End of inner exception stack trace --- at SkiaSharp.Views.Blazor.SKCanvasView.CreateBitmap(SKSizeI& unscaledSize) at SkiaSharp.Views.Blazor.SKCanvasView.OnRenderFrame() at SkiaSharp.Views.Blazor.Internal.ActionHelper.Invoke() at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) --- End of stack trace from previous location --- at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.InvokeSynchronously(JSRuntime jsRuntime, DotNetInvocationInfo& callInfo, IDotNetObjectReference objectReference, String argsJson) at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.Invoke(JSRuntime jsRuntime, DotNetInvocationInfo& invocationInfo, String argsJson) at Microsoft.AspNetCore.Components.WebAssembly.Services.DefaultWebAssemblyJSRuntime.InvokeDotNet(String assemblyName, String methodIdentifier, String dotNetObjectId, String argsJson) Uncaught Error: System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception. ---> System.DllNotFoundException: libSkiaSharpThe thread 0x5030 has exited with code 0 (0x0).

I have probably missed something Any Idea? Regards Peder

artemiusgreat commented 2 years ago

Try this as an example. You may need to downgrade the package version in csproj file and add additional nuget sources in nuget.config https://github.com/mattleibow/SkiaSharpBlazorWebAssembly

peder1896 commented 2 years ago

Thank you artemiusgreat i did as you suggested and downloaded the project, it worked perfect. I then took a look in the csproj file and realised i had not included the following in the PropertyGroup

<TargetFramework>net6.0</TargetFramework>
  <Nullable>enable</Nullable>
  <WasmBuildNative>true</WasmBuildNative>

and this in the ItemGroup

    <NativeFileReference Include="$(SkiaSharpStaticLibraryPath)\2.0.23\*.a" />

Now it works in my own project Great