-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
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. |
It would appear that you're using the Svg.NET library, and if that's the case, the error you're encountering occurs because Since you already have the SVG content in a string format ( 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 Here’s how you can modify your code to use the // 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);
}
} |
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. |
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
The text was updated successfully, but these errors were encountered: