You are currently viewing Generate QRCode with text on the left and right

Generate QRCode with text on the left and right

I am working on a project (ASP.NET Core) and one of the functionality in the project is to generate QR Code. The company who will print these QR Codes asks me to write 2 texts on the left and right hand side of the QR Code. One of the is the URL of the site; and the other is simply “Scan Me.”

Here is the example: QR Code

Generating a QR Code in ASP.NET is pretty simple and there are tons of libraries that you can use (most are free too). I picked the .Net.Codecreate.QRCodeGenerate (an open source, free Nuget library).
To generate the QR code:

var generatedCode = QrCode.EncodeText(qrCodeText, QrCode.Ecc.High);
byte[] png = generatedCode.ToPng(20, 10);
System.IO.File.WriteAllBytes(fileFullPath, png);

FileFullPath is a folder in the ASP.NET Core project, and I save the QR Code as a png file. Now the request is to add those 2 texts on the left and right side of the QR Code. This is image editing more than the QR Code editing as we save the code as an image.
The text we will be putting around the QR Code is rotated 270 degrees; we can use Graphics class from System.Drawing to transform the text.

Bitmap b = new Bitmap(image);
Graphics graphics = Graphics.FromImage(b);
graphics.TranslateTransform(b.Width / 2, b.Height / 2);
graphics.RotateTransform(270);
Font font = new Font(FontFamily.GenericSansSerif, 80, FontStyle.Regular, GraphicsUnit.Pixel, 1, true);


We load the QR Code image into bitmap class; get a hold to the Graphics, and put the center of the image as our origin coordinates.
After this we rotate the text 270 degrees and define our format that we will be using to write the text.
We want to write the text centrally aligned with the image so we need to measure the length of the text :

String strUrl = “QRCOLLAR.COM”;
SizeF sizeUrl = graphics.MeasureString(strUrl,font);
String strScanMe = “SCAN ME”;
SizeF sizeScanMe = graphics.MeasureString(strScanMe,font);

Now it is time to draw these texts on the image; there is a bit calculation, I will leave that part to you 🙂
graphics.DrawString(strUrl,font,Brushes.Black,-(sizeUrl.Width/2),-(b.Width/2)+20);
graphics.DrawString(strScanMe, font, Brushes.Black, -(sizeScanMe.Width / 2), (b.Width / 2) -160);


Leave a Reply