QRCoder icon indicating copy to clipboard operation
QRCoder copied to clipboard

Trying to Convert a SVG QRCODE to PNG file

Open PEQSPC opened this issue 2 years ago • 1 comments

I am having a error on this line of code ,var svgDocument = SvgDocument.Open(stream); That is saying -> CS1503 Argument 1: cannot convert from 'System.IO.MemoryStream' to 'string´ Help Pls

// Define the text for the QR code
                string qrText = $"{Part_Number};{resultado};{material}";
                using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
                {
                    QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
                    using (SvgQRCode qrCode = new SvgQRCode(qrCodeData))
                    {
                        string qrCodeAsSvg = qrCode.GetGraphic(2);
                        string fileName = $"{Part_Number}_QTYUNIT.svg";
                        string filePath = Path.Combine(folderPath, fileName);
                        // Save the SVG string to a file
                        File.WriteAllText(filePath, qrCodeAsSvg);

                        var byteArray = Encoding.ASCII.GetBytes(qrCodeAsSvg);
                        using (var stream = new MemoryStream(byteArray))
                        {
                            var svgDocument = SvgDocument.Open(stream);
                            var bitmap = svgDocument.Draw();
                            bitmap.Save(filePath, ImageFormat.Png);
                        }
                    }
                }

PEQSPC avatar Nov 16 '23 09:11 PEQSPC

Hello. It looks like SvgDocument.Open() takes a string parameter rather than a MemoryStream object. It probably wants the file path to the file you want to open, although I don't know the package you're using to parse SVG Documents, so it's hard to say for sure.

JoshnaksPNG avatar Jan 23 '24 21:01 JoshnaksPNG

It would appear that you're using the Svg.NET library, and if that's the case, the error you're encountering occurs because SvgDocument.Open expects a string path to a file, not a MemoryStream.

Since you already have the SVG content in a string format (qrCodeAsSvg), you can directly load the SVG from the string, rather than going through a memory stream. Svg.NET has a method to open SVG content from a string directly, specifically SvgDocument.FromSvg<SvgDocument>(string contents).

Here's how you can modify your code to use the SVG content directly:

// Define the text for the QR code
string qrText = $"{Part_Number};{resultado};{material}";
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
    QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
    using (SvgQRCode qrCode = new SvgQRCode(qrCodeData))
    {
        string qrCodeAsSvg = qrCode.GetGraphic(2);
        string fileName = $"{Part_Number}_QTYUNIT.svg";
        string filePath = Path.Combine(folderPath, fileName);
        // Save the SVG string to a file
        File.WriteAllText(filePath, qrCodeAsSvg);

        // Load SVG from string
        var svgDocument = SvgDocument.FromSvg<SvgDocument>(qrCodeAsSvg);
        var bitmap = svgDocument.Draw();
        string pngFilePath = Path.Combine(folderPath, $"{Part_Number}_QTYUNIT.png");
        bitmap.Save(pngFilePath, ImageFormat.Png);
    }
}

You can also save your QR code directly as a PNG file using QRCoder's PngByteQRCode class without involving the Svg.NET library, simplifying your approach. The PngByteQRCode class allows you to generate a PNG image in byte array form directly from the QR code data.

Here’s how you can modify your code to use the PngByteQRCode class:

// Define the text for the QR code
string qrText = $"{Part_Number};{resultado};{material}";
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
    QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
    
    // Create a PngByteQRCode object using the QRCodeData
    using (PngByteQRCode pngQrCode = new PngByteQRCode(qrCodeData))
    {
        // Generate the QR code as a PNG byte array
        byte[] qrCodeAsPngBytes = pngQrCode.GetGraphic(20);  // 20 can be adjusted based on the pixel size you want
        
        string fileName = $"{Part_Number}_QTYUNIT.png";
        string filePath = Path.Combine(folderPath, fileName);
        
        // Save the PNG byte array to a file
        File.WriteAllBytes(filePath, qrCodeAsPngBytes);
    }
}

Shane32 avatar Apr 13 '24 14:04 Shane32

Alternatively, if you want SVG and PNG output, you can just render the QRCodeData twice:

// Define the text for the QR code
string qrText = $"{Part_Number};{resultado};{material}";
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
    QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
    using (SvgQRCode qrCode = new SvgQRCode(qrCodeData))
    {
        string qrCodeAsSvg = qrCode.GetGraphic(2);
        string fileName = $"{Part_Number}_QTYUNIT.svg";
        string filePath = Path.Combine(folderPath, fileName);
        // Save the SVG string to a file
        File.WriteAllText(filePath, qrCodeAsSvg);

        using (PngByteQRCode pngQrCode = new PngByteQRCode(qrCodeData))
        {
            // Generate the QR code as a PNG byte array
            byte[] qrCodeAsPngBytes = pngQrCode.GetGraphic(20);  // 20 can be adjusted based on the pixel size you want
            string fileName = $"{Part_Number}_QTYUNIT.png";
            string filePath = Path.Combine(folderPath, fileName);
            // Save the PNG byte array to a file
            File.WriteAllBytes(filePath, qrCodeAsPngBytes);
        }
    }   
}

If none of the shown solutions from @Shane32 and me works for you, feel free to re-open the issue.

codebude avatar Apr 17 '24 22:04 codebude