in .net core, c#, dotnet

Generate qr code c# .net (for url for example. SVG)


Qr code are very famouse nowadays so I will teach you how to generate qr code using c# and net core. I will do an example how to encode a url.

It is very simple. Of course it is simple because somebody works hard and developed a library for us.

This library is Qr coder. It is defined like “A pure C# Open Source QR Code implementation“. You can see the full documentation and source code in github.

I am using net core 7 at the moment I write this article.

If you want to know more details about Qr code and how to it works, you can read a very good definition in wikipedia.

private static void Main(string[] args){
        Console.WriteLine("QR Codes");

        Url generator = new Url("https://www.developingthedeveloper.dev/");
        string payload = generator.ToString();

        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
        SvgQRCode qrCode = new SvgQRCode(qrCodeData);
        string qrCodeAsSvg = qrCode.GetGraphic(20);

        string file = @"c:/Home/Blog/example.svg";
        byte[] data = Encoding.ASCII.GetBytes(qrCodeAsSvg);

        System.IO.File.WriteAllBytes(file, data);
}