SkiaSharp
SkiaSharp copied to clipboard
[BUG] The type initializer for 'SkiaSharp.SKData' threw an exception (even after applying the solution of issue #2124).
Description
I'm trying to resize images (actually making thumbnail of 200x150) using SkiaSharp. The code works fine in my local setup which has Visual Studio Code 1.77.3
, .NET 7.0.400
and Windows 10 OS
. But when I deply the build of the code to Google Cloud Plateform (GCP)
with docker, the above mentioned exception occurs.
I tried the GCP deployment with following packages:
Note: The deployed code was written in Microsoft Visual Studio Community 2022 (64 bit)
, .NET 6
, Windows 10
.
SkiaSharp (2.88.5) SkiaSharp.Views.Blazor (2.88.5) (Solution for issue #2124) SkiaSharp.NativeAssets.Linux (2.88.5) SkiaSharp.NativeAssets.Linux.NoDependencies (2.88.5) SkiaSharp.NativeAssets.WebAsssembly (2.88.5)
On my local, the SkiaSharp (2.88.5)
alone worked.
Code
public IFormFile getThumbnailFromBase64(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
SKImage image = SKImage.FromEncodedData(imageBytes);
SKBitmap imageBitmap = SKBitmap.FromImage(image);
SKImageInfo info = new SKImageInfo(200, 150);
SKFilterQuality quality = SKFilterQuality.High;
SKBitmap thumbnail = imageBitmap.Resize(imageInfo, quality);
SKData thumbData = thumbnail.Encode(SKEncodedImageFormat.Jpeg, 100);
byte[] thumbByte = thumbData.ToArray();
var stream = new MemoryStream(thumbByte);
IformFile thumbFile = new FormFile(stream, 0, thumbByte.Length, "ImageFile", "ImageFile");
return thumbFile;
}
Expected Behavior
The code should take base64
image string and return an object of IFormFile
after deploying to GCP.
Also the exception should give more description or reason of the error and possible suggestions.
What this "type initializer" means in the exception?
Actual Behavior
I put logger after each line of the above code. The only logger output I got is after byte[] imageBytes = Convert.FromBase64String(base64String);
line. I my thinking the exception The type initializer for 'SkiaSharp.SKData' threw an exception
occured at the line where I used SKImage
class.
Version of SkiaSharp
Other (Please indicate in the description)
Last Known Good Version of SkiaSharp
Other (Please indicate in the description)
IDE / Editor
Visual Studio (Windows)
Platform / Operating System
Linux, Windows, Other (Please indicate in the description)
Platform / Operating System Version
- Google Cloud Plateform (Linux OS)
Devices
- Cloud Server (GCP)
Relevant Screenshots
..
Relevant Log Output
The type initializer for 'SkiaSharp.SKData' threw an exception.
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
The "type initializer" is just the runtime component that constructs objects. So, the error is saying there was an exception in the constructor.
So, this exception is saying it was unable to load the libSkiaSharp native binary.
Are you specifically using Blazor? Or, just web assembly?
Or, is this a native Linux app running in docker and the exception is happening on the server?
Hi @mattleibow, Thanks for the quick look up on the issue.
I am not using Blazor or Web assembly specifically. The same code with only SkiaSharp (2.88.5)
is working as expected on my Windows 10 console app with .NET 6. As per the suggestions across the web, I added all above mentioned SkiaSharp packages hoping atleast any of them might be the right one.
Yes, it is deployed to GCP (Google cloud platforms) Container Registry and build with docker. The exception is happening only on the server i.e GCP.
Am I referencing to a wrong packege ? Or some plateform compatibility issues?
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.
I have the same problem... the only difference is that i'am running my api in AWS ECS (Docker). i've installed the same SkiaSharp.NativeAssets.Linux and my docker file contain this: RUN apt-get install -y libfontconfig1 RUN apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev
I don't know what to do else.
You should add reference to SkiaSharp.NativeAssets.Linux.NoDependencies: SkiaSharp.NativeAssets.Linux.NoDependencies.
You should add reference to SkiaSharp.NativeAssets.Linux.NoDependencies: SkiaSharp.NativeAssets.Linux.NoDependencies.
not work
You should add reference to SkiaSharp.NativeAssets.Linux.NoDependencies: SkiaSharp.NativeAssets.Linux.NoDependencies.
This worked for me after adding missing 2 libs (in case)
libfontconfig1 \
libfreetype6 \
&& rm -rf /var/lib/apt/lists/*```
into the DockerFile.
Thanks, a fixed de problem with your recommendation. very kind of you
El jue, 18 ene 2024 a las 0:33, Atakan Eser @.***>) escribió:
You should add reference to SkiaSharp.NativeAssets.Linux.NoDependencies: SkiaSharp.NativeAssets.Linux.NoDependencies https://www.nuget.org/packages/SkiaSharp.NativeAssets.Linux.NoDependencies .
This worked for me after adding missing 2 libs (in case)
libfontconfig1 \ libfreetype6 \ && rm -rf /var/lib/apt/lists/*``` into the DockerFile.
— Reply to this email directly, view it on GitHub https://github.com/mono/SkiaSharp/issues/2607#issuecomment-1897830156, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKLZFU54NRSLVY3J6SSPGVDYPCX2RAVCNFSM6AAAAAA4XCQM22VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJXHAZTAMJVGY . You are receiving this because you commented.Message ID: @.***>