Graphics format PNG not available (are you missing the NuGet package Codecrete.SwissQRBill.Generator?)
Having upgraded from Codecrete.SwissQRBill.Generator 2.5.3 to version 3.3.0 I receive the above mentioned error when attempting to generate a .png type of image (GraphicsFormat = GraphicsFormat.PNG)
What am I missing?
The error message sounds as if you are using the Codecrete.SwissQRBill.Core instead of Codecrete.SwissQRBill.Generator. The former one does not include PNG generation.
If this is not the case, then please describe the technical aspects of the application you are building:
- Is it a standalone .exe, an ASP.NET application, a library etc.?
- What platform (Windows, Linux, macOS) does it run on?
- What version of .NET are you using?
- Are you using trimming or a similar technique to optimize the executable?
Codecrete.SwissQRBill.Generator determines at run-time if the PNG generation capability is available. If it can't find a class providing it, it will generate the mentioned error message.
I've tried both Codecrete.SwissQRBill.Generator as well as Codecrete.SwissQRBill.Windows I am targeting WPF app for Windows 10 (SwissQRBill is referenced in the companion class library project). As far as I have now discovered the error occurs when I build for .Net Framework 4.8 When compiled for .NET 8 the PNG format is handled correctly. SVG format is handled ok in both cases.
PS Exception HResult=0x80131500 Exception stack trace: (last line - or actually the first 🙂 - is my code)
at Codecrete.SwissQRBill.Generator.QRBill.CreateCanvas(BillFormat format) at Codecrete.SwissQRBill.Generator.QRBill.Generate(Bill bill) at RdExtensions.QRBillService.GetQRFileName(Invoice invoice)
Your case sound familiar. We've seen it before. If Codecrete.SwissQRBill.Windows is indirectly referenced (e.g. Application -> Class Library Project -> SwissQRBill package), then Visual Studio seems to optimize Codecrete.SwissQRBill.Generator or Codecrete.SwissQRBill.Windows away and only reference Codecrete.SwissQRBill.Core.
To prevent it, add the following code to the class generating the QR bill:
new PNGCanvasFactory().Register();
I've taken the case to extend the README. See https://github.com/manuelbl/SwissQRBill.NET?tab=readme-ov-file#png-generation.
The better code to include might be CanvasCreator.Register(new PNGCanvasFactory());. It works with both Codecrete.SwissQRBill.Generator or Codecrete.SwissQRBill.Windows.
Thanks a lot! This workaround does it! 👍