codebude/QRCoder

The type initializer for 'SkiaSharp.SKData' threw an exception.

Closed this issue · 3 comments

This is my code.

QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrData.Join("\n"), QRCodeGenerator.ECCLevel.Q);
BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData);

byte[] qrCodeBytes = qrCode.GetGraphic(20);
using (SKBitmap skBitmap = SKBitmap.Decode(qrCodeBytes))
{

 var directory = Path.Combine(Environment.CurrentDirectory, "Core/Uploads/Visitor/");
 if (!Directory.Exists(directory))
 {
     Directory.CreateDirectory(directory);
 }

 var path = Path.Combine(directory, $"{data.RequestNumber}.png");

 using (var fileStream = System.IO.File.OpenWrite(path))
 {
     skBitmap.Encode(SKEncodedImageFormat.Png, 100).SaveTo(fileStream);
 }

}

giving error : The type initializer for 'SkiaSharp.SKData' threw an exception.

After Deployment on Linux server.

Version: .Net6

Hi @SudhanshuAESS ,

Can you please give more details and/or a stack trace? Per my belief BitmapByteQRCodes are plain standard bitmaps and should be readable by SkiaSharp. Maybe the exception is triggered by another part of your code?

Looks like a SkiaSharp issue to me. Type initializers are, I think, the static constructor, so code for the SkiaSharp.SKData class probably exists in the SkiaSharp library.

Keep in mind that QRCoder can encode PNG files directly via the PngByteQRCode class, which is supported on Linux. No real need to run it through SkiaSharp if you just want to save a png.

Due to lack of details and missing feedback I'll close the issue. Feel free to re-open it, in case you still have problems/questions @SudhanshuAESS