PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Unsupported image format exception
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4523
Page 1 of 1

Author:  stefancata26 [ Fri Dec 22, 2023 1:10 pm ]
Post subject:  Unsupported image format exception

I am trying to print a QR code to a pdf document. I used the QRCoder library to convert a string to png byte array.
Why is the line "XImage qrCodeImage = XImage.FromStream(qrCodeStream);" throwing an "Unsupported image format." exception?
This was working in PDF sharp version 1.50.5147, but after upgrading to version 6.0.0 it stopped working.


public class GeneratePdf
{
byte[] pdfData;

public byte[] PaymentLinkToPngQrCode(string paymentLink)
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(paymentLink, QRCodeGenerator.ECCLevel.Q);
PngByteQRCode qrCode = new PngByteQRCode(qrCodeData);
byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(10);

return qrCodeAsPngByteArr;
}
public async Task<byte[]> GeneratePdfWithQrCode(string paymentLink)
{
try
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);

// Generate the QR code image from the payment link
byte[] qrCodeAsPngByteArr = PaymentLinkToPngQrCode(paymentLink);

// Create a bitmap from the QR code byte array
using (var qrCodeStream = new MemoryStream(qrCodeAsPngByteArr, 0, qrCodeAsPngByteArr.Length, false, true))
{
XImage qrCodeImage = XImage.FromStream(qrCodeStream);

// Draw the QR code image on the PDF page
gfx.DrawImage(qrCodeImage, new XRect(100, 100, 100, 100));

using (MemoryStream pdfStream = new MemoryStream())
{
// Save the PDF document into the MemoryStream
document.Save(pdfStream, false);

// Get the PDF content as a byte array
return pdfStream.ToArray();
}
Console.WriteLine("PDF saved successfully.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return null;
}
}

Author:  malaneha [ Sat Dec 23, 2023 7:45 am ]
Post subject:  Re: Unsupported image format exception

Use XImage to ensure compatibility.Using an image format supported by fromstream such as PNG. Downgrade to PDF Sharp version 1.50.5147 or verify that QR code conversion meets the image format criteria of the newest version.

Thak you.

Author:  TH-Soft [ Sat Dec 23, 2023 6:40 pm ]
Post subject:  Re: Unsupported image format exception

As I wrote on GitHub: Use PDFsharp 6.0.0 GDI build to get the full image support of PDFsharp 1.50.

When using PDFsharp 6.0.0 Core build, convert your PNG image to a supported PNG or BMP format.

A list of image formats supported by the Core build can be found here:
https://docs.pdfsharp.net/PDFsharp/Over ... tions.html

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/