Article directory
- foreword
- 1. Two-dimensional code generator QRCoder
- 2. Add text description below the QR code
foreword
A QR code is a coded form capable of storing data that can be quickly recognized and decoded by a scanner. QR codes are usually composed of black and white squares, which can encode information such as text, URLs, emails, phone numbers, etc. into a matrix barcode form, making it easier for users to obtain information. Two-dimensional codes are used in a wide range of applications, such as business, advertising, news, public transportation systems and other fields.
A barcode, also known as a commodity barcode, is a one-dimensional barcode based on a bar pattern, and is a coding method widely used in the fields of commerce and logistics. It can quickly and accurately read product information, including product name, price, brand, production date, etc., and plays a huge role in tracing product circulation, improving logistics efficiency, and preventing counterfeit and shoddy products.
Sorry, because csdn will block pictures with QR codes, some of the pictures below cannot be viewed.
1. Two-dimensional code generator QRCoder
1. What is QRCode
QRCoder is a simple library written in C#.NET that enables you to create QR codes. It does not have any dependencies on other libraries. It is provided as .NET Framework and .NET Core PCL versions on NuGet, and you can customize the QR code of attributes such as pixels, colors, background colors, logos, logo sizes, and logo borders.
In addition to the QR code format of the secondary bitmap, the following formats are also supported:
- QRCode
- ArtQRCode
- AsciiQRCode
- Base64QRCode
- BitmapByteQRCode
- PdfByteQRCode
- PngByteQRCode
- PostscriptQRCode
- SvgQRCode
- UnityQRCode
- XamlQRCode
The library supports QR codes for the following structured content:
- payment code
- Bitcoin-Like cryptocurrency payment address: virtual currency payment address
- Bookmark: bookmark
- Calendar events (iCal/vEvent): calendar events
- ContactData(MeCard/vCard): contact data
- Geolocation: Location
- Girocode
- Email: mail
- MMS: Multimedia Messaging Service
- Monero address/payment
- One-Time-Password: one-time password
- Phonenumber: mobile phone number
- RussiaPaymentOrder
- Shadowsocks configuration
- Skype
- SlovenianUpnQr
- SMS: short message
- SwissQrCode
- Url: URL
- WiFi
QRCoder's code URL: https://github.com/codebude/QRCoder
2. Installation package
Install-Package QRCoder
Adjust OS to windows operating system
3. Ordinary QR code
3.1 Create a QR code
using QRCoder;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
3.2 Set the QR code color
using QRCoder;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
//设置颜色
//Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, false);
Bitmap qrCodeImage = qrCode.GetGraphic(10, "#efefef", "#2e3e4e");
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
3.3 QR code with logo
1. Set the logo
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
//设置logo
//Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Bitmap.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
2. Set the border of the logo icon
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
//设置logo
//Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Bitmap.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"), iconBorderWidth: 6);
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
3. Set the background color of the border of the logo icon
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
//设置logo
//Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Bitmap.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"), iconBorderWidth: 6, iconBackgroundColor: Color.DarkGreen);
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
4. Artistic QR Code
4.1 Create a QR code
using QRCoder;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var bmp = new ArtQRCode(data).GetGraphic(10);
bmp.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
4.2 Set QR code color and image size
using QRCoder;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var finder = new Bitmap(15, 15);
var bmp = new ArtQRCode(data).GetGraphic(10, Color.Black, Color.White, Color.Transparent, finderPatternImage: finder);
bmp.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
4.3 Full screen QR code
using QRCoder;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var bmp = new ArtQRCode(data).GetGraphic(10, Color.Black, Color.White, Color.Transparent, drawQuietZones: false);
bmp.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
4.4 QR code with logo
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var bmp = new ArtQRCode(data).GetGraphic((Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
bmp.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
5. Plain text QR code
5.1 Create a QR code
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
var targetCode = " \n \n \n \n ██████████████ ██ ██ ██████████████ \n ██ ██ ██ ████ ██ ██ \n ██ ██████ ██ ██ ██ ██ ██ ██████ ██ \n ██ ██████ ██ ██ ██ ██████ ██ \n ██ ██████ ██ ██ ██ ██████ ██ \n ██ ██ ████████ ██ ██ \n ██████████████ ██ ██ ██ ██████████████ \n ██ ████ \n ██████████ ████ ████████ ██ ████ \n ████ ██ ██ ████ ████████ ██ \n ██ ██ ██████████ ██ ██ ██ ████ \n ██ ██ ████ ████ ████ \n ████████ ██████ ████ ██ ██ \n ████████ \n ██████████████ ████ ████ ██ ████ ████ \n ██ ██ ████████ \n ██ ██████ ██ ██ ██ ██ ██ ██ ██ \n ██ ██████ ██ ██████ ██ ██ \n ██ ██████ ██ ██ ██ ██ ██ ████ ████ \n ██ ██ ████ ████ ██ ██ \n ██████████████ ██████ ██ ██████ \n \n \n \n ";
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(1);
5.2 Full screen QR code
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
var targetCode = "██████████████ ██ ██ ██████████████\n██ ██ ██ ████ ██ ██\n██ ██████ ██ ██ ██ ██ ██ ██████ ██\n██ ██████ ██ ██ ██ ██████ ██\n██ ██████ ██ ██ ██ ██████ ██\n██ ██ ████████ ██ ██\n██████████████ ██ ██ ██ ██████████████\n ██ ████ \n██████████ ████ ████████ ██ ████ \n████ ██ ██ ████ ████████ ██\n ██ ██ ██████████ ██ ██ ██ ████ \n██ ██ ████ ████ ████ \n ████████ ██████ ████ ██ ██\n ████████ \n██████████████ ████ ████ ██ ████ ████\n██ ██ ████████ \n██ ██████ ██ ██ ██ ██ ██ ██ ██\n██ ██████ ██ ██████ ██ ██ \n██ ██████ ██ ██ ██ ██ ██ ████ ████\n██ ██ ████ ████ ██ ██ \n██████████████ ██████ ██ ██████";
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(1, drawQuietZones: false);
5.3 X QR code
using QRCoder;
//创建二维码
var targetCode = " \n \n \n \n \n \n \n \n XXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXX \n XXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXX \n XX XX XXXXXX XX XX XX \n XX XX XXXXXX XX XX XX \n XX XXXXXX XX XXXXXXXX XX XXXXXX XX \n XX XXXXXX XX XXXXXXXX XX XXXXXX XX \n XX XXXXXX XX XXXX XX XXXXXX XX \n XX XXXXXX XX XXXX XX XXXXXX XX \n XX XXXXXX XX XX XX XX XXXXXX XX \n XX XXXXXX XX XX XX XX XXXXXX XX \n XX XX XX XX XX \n XX XX XX XX XX \n XXXXXXXXXXXXXX XX XX XX XXXXXXXXXXXXXX \n XXXXXXXXXXXXXX XX XX XX XXXXXXXXXXXXXX \n XXXXXXXX \n XXXXXXXX \n XX XXXXXX XXXXXX XX XX XX \n XX XXXXXX XXXXXX XX XX XX \n XX XXXXXX XXXX XXXXXXXX XXXXXX XX \n XX XXXXXX XXXX XXXXXXXX XXXXXX XX \n XX XX XX XX XX XX \n XX XX XX XX XX XX \n XX XX XX XX XXXXXX \n XX XX XX XX XXXXXX \n XX XXXXXXXX XXXX XX XXXXXXXX XX \n XX XXXXXXXX XXXX XX XXXXXXXX XX \n XX XXXXXXXX XXXX \n XX XXXXXXXX XXXX \n XXXXXXXXXXXXXX XXXXXXXX XX XXXXXX \n XXXXXXXXXXXXXX XXXXXXXX XX XXXXXX \n XX XX XXXXXX XXXXXXXX \n XX XX XXXXXX XXXXXXXX \n XX XXXXXX XX XX XXXX XX XXXX \n XX XXXXXX XX XX XXXX XX XXXX \n XX XXXXXX XX XXXX XXXXXXXX \n XX XXXXXX XX XXXX XXXXXXXX \n XX XXXXXX XX XX XXXXXXXX XX XXXXXX \n XX XXXXXX XX XX XXXXXXXX XX XXXXXX \n XX XX XX XXXX XX \n XX XX XX XXXX XX \n XXXXXXXXXXXXXX XX XXXXXX XXXX XXXX \n XXXXXXXXXXXXXX XX XXXXXX XXXX XXXX \n \n \n \n \n \n \n \n ";
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(2, "X", " ");
6. Bitcoin Generator
using QRCoder;
//创建二维码
var address = "175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W";
var amount = .123;
var label = "Some Label to Encode";
var message = "Some Message to Encode";
var generator = new PayloadGenerator.BitcoinAddress(address, amount, label, message).ToString();
Here is not only Bitcoin, but also built-in various account generation (Litecoin, WIFI account password, email account password, etc.)
7. Byte QR code
using QRCoder;
using System;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] {
255, 255, 255, 127 }, new byte[] {
0, 0, 255 }, false);
MemoryStream ms = new MemoryStream(pngCodeGfx);
Bitmap img = (Bitmap)Image.FromStream(ms);
img.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
8. Two-dimensional data generation
var gen = new QRCodeGenerator();
var checkString = string.Empty;
var gField = gen.GetType().GetField("galoisField", BindingFlags.NonPublic | BindingFlags.Static);
foreach (var listitem in (System.Collections.IEnumerable)gField.GetValue(gen))
{
foreach (PropertyInfo prop in listitem.GetType().GetProperties())
checkString += prop.GetValue(listitem, null).ToString() + ",";
checkString += ":";
}
Also contains other ways like text
9. SVG QR code
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
//Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909361
var logoSvg = File.ReadAllText(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_Scientist_2909361.svg");
var logoObj = new SvgQRCode.SvgLogo(logoSvg, 20, iconEmbedded: false);
var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj);
Just hang it on the website
10. WIFI QR code
using QRCoder;
using static QRCoder.PayloadGenerator;
WiFi generator = new WiFi("WIFI名称","WIFI密码",WiFi.Authentication.WPA);
string payload = generator.ToString();
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
var qrCodeAsBitmap = qrCode.GetGraphic(20);
qrCodeAsBitmap.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
11. Email QR code
using QRCoder;
using static QRCoder.PayloadGenerator;
Mail generator = new Mail("邮件地址","邮件标题", "邮件标题");
string payload = generator.ToString();
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
var qrCodeAsBitmap = qrCode.GetGraphic(20);
qrCodeAsBitmap.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
2. Add text description below the QR code
public class ImageUtility
{
/// <summary>
/// 二维码下面加上文字
/// </summary>
/// <param name="qrImg">QR图片</param>
/// <param name="content">文字内容</param>
/// <param name="n"></param>
/// <returns></returns>
public Bitmap InsertWords(Bitmap qrImg, string content = "")
{
Bitmap backgroudImg = new Bitmap(qrImg.Width, qrImg.Height);
backgroudImg.MakeTransparent();
Graphics g2 = Graphics.FromImage(backgroudImg);
g2.Clear(Color.Transparent);
//画二维码到新的面板上
g2.DrawImage(qrImg, 0, 0);
if (!string.IsNullOrEmpty(content))
{
FontFamily fontFamily = new FontFamily("楷体");
Font font1 = new Font(fontFamily, 20f, FontStyle.Bold, GraphicsUnit.Pixel);
//文字长度
int strWidth = (int)g2.MeasureString(content, font1).Width;
//总长度减去文字长度的一半 (居中显示)
int wordStartX = (qrImg.Width - strWidth) / 2;
int wordStartY = qrImg.Height - 30;
g2.DrawString(content, font1, Brushes.Black, wordStartX, wordStartY);
}
g2.Dispose();
return backgroudImg;
}
}