Generating QR Code In C#

Introduce

A QR code (quick response code) is a two dimensional barcode that stores information about the item to which it is attached. The information can be plain text, URL, email address, vCard information, etc.

In this article, you’ll learn how to generate QR code image in C# using free Spire.Barcode instead of using any ‘barcode fonts’. The reasons are that the code to create QR code image with Spire.Barcode is pretty simply, this component also enables programmers to draw most common linear and 2D barcodes besides QR code, and it is totally free for both personal and commercial use.

In the following section, I created a Windows Form Application (QR code generator) to demonstrate how to create customized QR codes according to your own needs and save them as high quality images for immediate use.

Now let’s check the main steps.

Step 1

Download Spire.Barcode from the link mentioned above. You can also find the library from Nuget and E-iceblue.

Step 2

Create a Windows Form Application project in VS, add the dll to your .NET project assemblies.

Step 3

Request a free key from sales team in E-iceblue. This library is free of charge, but you need a free key to remove the ‘E-iceblue’ logo in barcode image. Otherwise, the logo will exist.

Once you get a key, you can apply the key by placing one line of code at the beginning of your program.

Spire.Barcode.BarcodeSettings.ApplyKey("your key");

Step 4

Design the form with some input fields for accepting parameters to customize your QR code, one Generate button to create QR code image and display it in a picture box, and one Save button to save the QR code image to local folder.

QR code image

Step 5

Using the code. In this case, I only focus on the code under btnGenerate click event. The entire code is available in the zip file attached.

Spire provides a class of BarcodeSettings in which all properties of a barcode can be found and reset. Here are some basic settings of my QR code.
  1. BarcodeSettings.ApplyKey("your key");  
  2. BarcodeSettings settings = new BarcodeSettings();  
  3. settings.Type = BarCodeType.QRCode;  
  4. settings.Unit = GraphicsUnit.Pixel;  
  5. settings.ShowText = false;  
  6. settings.ResolutionType = ResolutionType.UseDpi;  
Collect data from the form and assign the data value to each parameter member of the BarcodeSetting object.
  1. //input data  
  2. string data = "12345";  
  3. settings.Data = data;  
  4. if (this.richTextBox1.Text != null && this.richTextBox1.Text.Length > 0) {  
  5.     data = this.richTextBox1.Text;  
  6.     settings.Data = data;  
  7. }  
  8. //set fore color  
  9. if (this.comboBoxForeColor.SelectedItem != null) {  
  10.     string foreColor = this.comboBoxForeColor.SelectedItem.ToString();  
  11.     settings.ForeColor = Color.FromName(foreColor);  
  12. }  
  13. //set back color  
  14. if (this.comboBoxBackColor.SelectedItem != null) {  
  15.     string backColor = this.comboBoxBackColor.SelectedItem.ToString();  
  16.     settings.BackColor = Color.FromName(backColor);  
  17. }  
  18. //set x  
  19. short barWidth;  
  20. if (this.textBoxX.Text != null && this.textBoxX.Text.Length > 0 && Int16.TryParse(this.textBoxX.Text, out barWidth)) {  
  21.     settings.X = barWidth;  
  22. }  
  23. //set left margin  
  24. short leftMargin = 1;  
  25. if (this.textBoxLeft.Text != null && this.textBoxLeft.Text.Length > 0 && Int16.TryParse(this.textBoxLeft.Text, out leftMargin)) {  
  26.     settings.LeftMargin = leftMargin;  
  27.   
  28. }  
  29. //set right margin  
  30. short rightMargin = 1;  
  31. if (this.textBoxRight.Text != null && this.textBoxRight.Text.Length > 0 && Int16.TryParse(this.textBoxRight.Text, out rightMargin)) {  
  32.     settings.RightMargin = rightMargin;  
  33.   
  34. }  
  35. //set top margin  
  36. short topMargin = 1;  
  37. if (this.textBoxTop.Text != null && this.textBoxTop.Text.Length > 0 && Int16.TryParse(this.textBoxTop.Text, out topMargin)) {  
  38.     settings.TopMargin = topMargin;  
  39.   
  40. }  
  41. //set bottom margin  
  42. short bottomMargin = 1;  
  43. if (this.textBoxBottom.Text != null && this.textBoxBottom.Text.Length > 0 && Int16.TryParse(this.textBoxBottom.Text, out bottomMargin)) {  
  44.     settings.BottomMargin = bottomMargin;  
  45.   
  46. }  
  47. //select correction level  
  48. if (this.comboBoxCorrectionLevel.SelectedItem != null) {  
  49.     int correctionLevel = this.comboBoxCorrectionLevel.SelectedIndex;  
  50.     switch (correctionLevel) {  
  51.         case 0:  
  52.             settings.QRCodeECL = QRCodeECL.L;  
  53.             break;  
  54.         case 1:  
  55.             settings.QRCodeECL = QRCodeECL.M;  
  56.             break;  
  57.         case 2:  
  58.             settings.QRCodeECL = QRCodeECL.Q;  
  59.             break;  
  60.         case 3:  
  61.             settings.QRCodeECL = QRCodeECL.H;  
  62.             break;  
  63.     }  
  64.   
  65. }  
Generate QR code image and display it in picture box.
  1. //generate QR code  
  2. BarCodeGenerator generator = new BarCodeGenerator(settings);  
  3. Image QRbarcode = generator.GenerateImage();  
  4. //display QR code image in picture box  
  5. pictureBox1.Image = QRbarcode;  
Output

Run the program and input some data as follows, click Generate and you’ll get the following output:

Output

Click Save to extract QR code image to local folder:

QR code

Thanks for reading, and hope this article can be of help to someone who is searching an easy solution in this field.

Up Next
    Ebook Download
    View all
    Learn
    View all