codebude/QRCoder

PlatformNotSupportedException when trying to generate QR Code.

Awesomer9561 opened this issue · 2 comments

I have a blazor web assembly project and I am trying to generate QR using this nuget package but it is always throwing PlatformNotSupportedException
Here is my code snippet :
if (_selectedBCard != null)
{
var inviteLink = string.Format(Constant.WebCardUrl, _selectedBCard.BcardId);

            var qrGenerator = new QRCodeGenerator();
            var qrCodeData = qrGenerator.CreateQrCode(inviteLink, QRCodeGenerator.ECCLevel.Q);
            var qrCode = new QRCode(qrCodeData);

            var base64Image = qrCode.GetGraphic(10);
            QRByte = $"data:image/png;base64,{base64Image}";
        }

I don't think that there's any other initialization required for it to work.

Here's the stack trace -
System.PlatformNotSupportedException: System.Drawing.Common is not supported on this platform.
at System.Drawing.Image..ctor()
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at QRCoder.QRCode.GetGraphic(Int32 pixelsPerModule, Color darkColor, Color lightColor, Boolean drawQuietZones)
at QRCoder.QRCode.GetGraphic(Int32 pixelsPerModule)
at company_portal.Pages.BcardsModel.GenerateQRCode() in C:\Users\pa740\source\repos\BusinessCode\company_portal\Pages\Bcards.razor.cs:line 392
at company_portal.Pages.Bcards.GenerateQRCode(CompanyBcardResponse bcard) in C:\Users\pa740\source\repos\BusinessCode\company_portal\Pages\Bcards.razor:line 407
at company_portal.Pages.Bcards.<>c__DisplayClass0_0.b__7() in C:\Users\pa740\source\repos\BusinessCode\company_portal\Pages\Bcards.razor:line 195
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[Object](MulticastDelegate delegate, Object arg)
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync(Object arg)
at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg)
at Microsoft.AspNetCore.Components.EventCallback.InvokeAsync(Object arg)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs)

Type of issue

[ ] Bug

Steps to Reproduce (for bugs)

Copy and paste the above code snippet.

Your Environment

  • Version used: 1.4.2, 1.4.3
  • Compiled from source or NuGet package?: Nuget package
  • Payload/Encoded text: Mentioned above
  • Used ECC-level: Q
  • Environment (.NET 3.5, .NET 4.X, .NETCore, ...): .NET 6

use the code below to generate png image as byte[]

using var qrGenerator = new QRCodeGenerator();
using var qrCodeData = qrGenerator.CreateQrCode(inviteLink, QRCodeGenerator.ECCLevel.Q);
using var qrCode = new PngByteQRCode(qrCodeData);
byte[] png = qrCode.GetGraphic(20);

you can then encode that into a data url

@Awesomer9561 Microsoft removed System.Drawing support in .NET 6 for non-Windows platforms. Since the QRCode renderer uses System.Drawing, it's correct that you see a PlatformNotSupportException. But as @timdinh said - that shouldn't be a problem as there are other rendering classes that are support on non-Windows platforms. Check the compatibility matrix to find a renderer that fits your platform/environment: https://github.com/codebude/QRCoder/wiki/Advanced-usage---QR-Code-renderers#2-overview-of-the-different-renderers