Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to Convert a SVG QRCODE to PNG file #478

Closed
PEQSPC opened this issue Nov 16, 2023 · 3 comments
Closed

Trying to Convert a SVG QRCODE to PNG file #478

PEQSPC opened this issue Nov 16, 2023 · 3 comments

Comments

@PEQSPC
Copy link

PEQSPC commented Nov 16, 2023

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);
                        }
                    }
                }
@JoshnaksPNG
Copy link

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.

@Shane32
Copy link
Contributor

Shane32 commented Apr 13, 2024

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);
    }
}

@codebude
Copy link
Owner

codebude commented Apr 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants