To Generate a QR Code with Text around it using ASP.Net, you can use the nuget package as follows.
We can use zxing.net, or QRCoder free nuget package,
In this sample, we are going with the QRCoder package.

Install the QRCoder package on our project. We are good to go now.
Generate QR Code Sample
In Below code snippet we are using QRCodeGenerator Class to generate QRCode, create an instance for QRCodeGenerator(),
QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q)
It will Create QRCode Data for the given text.
ECCLevel
Error Correction Level of the QR code. It represents the ability of the QR code to remain readable even if it's partially damaged or obscured.
- L (Low): About 7% of the codewords can be restored.
- M (Medium): About 15% of the codewords can be restored.
- Q (Quartile): About 25% of the codewords can be restored.
- H (High): About 30% of the codewords can be restored.
It will create QRCode for QR Code data,
QRCode qrCode = new QRCode(qrCodeData);
Converting QR Code into bitmap
GetGraphic(int x);
x: Pixel rate for that QR Code
And we have plenty of override methods for GetGraphic() for different use case scenarios.
Bitmap bitMap = qrCode.GetGraphic(20)
Converting bitmap to base64 string
In bitMap.Save() helps us to save the bitmap in image format.
System.Drawing.Imaging.ImageFormat: we can choose which format we would like to save our image, like (PNG, GIF, JPEG, Icons....etc).
And we are converting our memorystream to byte array then we can convert it into string with base64.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
string base64String = Convert.ToBase64String(byteImage);
Generate Url for QR Code Image
We can define data while returning from API calls, or we can handle that on the UI side.
ImageUrl = "data:image/png;base64," + base64String;
If we just return base64String.
<img [src]="'data:image/png;base64,'+ base64String" />
Sample for Generate QR Code with text around it.
In Below Sample GetGraphic(7, Color.Black, Color.White, null, 7, 3).
Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon = null, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true).
- 7: pixelsPerModule
- Black: darkColor
- White: lightColor
- null: icon
- 7: iconSizePercent
- 3: iconBorderWidth
public string GenerateRecurringQRCode(string key){
string result = string.Empty;
QRCodeGenerator qrGenerator = new QRCodeGenerator()
QRCodeData qrCodeData = qrGenerator.CreateQrCode(key, QRCodeGenerator.ECCLevel.Q)
using (QRCode qrCode = new QRCode(qrCodeData))
{
using (Bitmap bitMap = qrCode.GetGraphic(7, Color.Black, Color.White, null, 7, 3))
{
using (MemoryStream ms = new MemoryStream())
{
Bitmap qrCodeWithText = AddTextToQRCode(bitMap); // Adding Text to QR Code
qrCodeWithText.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
result = Convert.ToBase64String(byteImage);
}
}
}
return result;
}
In this code snippet below we have drawn a DO NOT SCREENSHOT QR CODE around qr code AddTextToQRCode();
static Bitmap AddTextToQRCode(Bitmap bitMap)
{
RectangleF rectf1 = new RectangleF(26, 8, 350, 100);
RectangleF rectf2 = new RectangleF(26, 263, 350, 100);
Graphics g = Graphics.FromImage(bitMap);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString("DO NOT SCREENSHOT QR CODE", new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, rectf1);
g.DrawString("DO NOT SCREENSHOT QR CODE", new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, rectf2);
Rectangle recleft = new Rectangle();
recleft.Height = 200;
recleft.Width = 500;
recleft.X = 0;
recleft.Y = 0;
SizeF s;
String str = "DO NOT SCREENSHOT QR CODE";
StringFormat strf = new StringFormat();
strf.Alignment = StringAlignment.Center;
recleft.X = 0;
recleft.Y = 0;
g.TranslateTransform(8, 393);
g.RotateTransform(-90);
g.DrawString(str, new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, recleft, strf);
g.ResetTransform();
Rectangle recright = new Rectangle();
recright.Height = 200;
recright.Width = 500;
recright.X = 0;
recright.Y = 0;
SizeF sright;
String strright = "DO NOT SCREENSHOT QR CODE";
StringFormat strfright = new StringFormat();
strf.Alignment = StringAlignment.Center;
recright.X = 0;
recright.Y = 0;
g.TranslateTransform(280, -104);
g.RotateTransform(90);
g.DrawString(str, new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, recright, strf);
g.ResetTransform();
g.Flush();
return bitMap;
}
Join the conversation! Your thoughts help the community grow.