Skip to content

lucasdsalves/qrcode-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

This API project shows how to generate a QRCode from a URL using QRCoder package.

Used packages

dotnet add package QRCoder

QRCode generate

ExtensionMethods.cs

 public static Bitmap GenerateImage(string url)
        {
            var qrGenerator = new QRCodeGenerator();
            var qrCode = new QRCode(qrGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q));

            return qrCode.GetGraphic(10);
        }

        public static byte[] GenerateByteArray(string url)
        {
            return ImageToByte(GenerateImage(url));
        }

        private static byte[] ImageToByte(Image img)
        {
            using var stream = new MemoryStream();

            img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

            return stream.ToArray();
        }

QrCodeAppService.cs

        public Task<byte[]> GenerateQrCodeFile(string url)
        {
            return Task.FromResult(ExtensionMethods.GenerateByteArray(url));
        }

QrCodeController.cs

        [HttpGet("generate")]
        public async Task<FileResult> GenerateQrCode(string url)
        {
            return File(await _qrCodeService.GenerateQrCodeFile(url), "image/png");
        }

Result available through API swagger endpoint /qrcode/generate

Releases

No releases published

Packages

No packages published

Languages