Skip to main content

C# QR Code Generator for .NET

C# QR Code Generator for .NETPhoto by Pixabay

Originally Posted On: https://ironsoftware.com/csharp/barcode/tutorials/csharp-qr-code-generator/

 

As you may have read in the Creating a Barcode introductory tutorial, creating, styling, and exporting barcodes as images with Iron Barcode is incredibly simple and may often be achieved in a single line of code.

In this example, we will look more in depth at QR codes, which are becoming increasingly popular in industrial applications and also in retail.

Installation

Before we start we need to install the BarCode Nuget Package.

PM > Install-Package Barcode

https://www.nuget.org/packages/BarCode/

As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from [.Net Barcode DLL].

Video Link

Importing NameSpaces

For this tutorial we need to make sure our class files reference IronBarCode and also some normal system assemblies.

using IronBarCode; using System; using System.Drawing; using System.Linq;

 

Create a QR code with 1 Line of Code

Our first example shows us how to create a standardized barcode with some simple text, a 500 pixel square diameter, with a medium error correction.

  1. // Generate a Simple BarCode image and save as PDF
  2. QRCodeWriter.CreateQrCode(“hello world”, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng(“MyQR.png”);

Copy code to clipboardVB  C#

Error correction allows us to define how easy it will be for a QR code to be read in real world circumstances. Higher error correction levels create larger QR codes with more pixels and more complexity.

C# Create QR Code Image

In the second example, we will look at a use case where a company wishes to add to a logo to their QR code, which is something we see commonly these days. By using the QRCodeWriter.CreateQRCodeWithLogo method, in the code sample below you can see how easy this is.

  1. // Adding a Logo
  2. var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo(“https://ironsoftware.com/csharp/barcode/”, “visual-studio-logo.png”, 500);
  3. MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);

Copy code to clipboardVB  C#

This example adds the Visual Studio logo to the barcode. It automatically sizes it to an appropriate size where the pure code is still readable and aligns that logo to the QR code square grid so that it looks appropriate. The next line of code colors the barcode dark green. However, it does not discolor the logo.

C# Create QR Code With Logo Image

Save as Image PDF or HTML

Finally, we save that QR as a PDF. The final line of code opens the PDF in your default PDF browser for you convenience.

  1. // Adding a Logo
  2. var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo(“https://ironsoftware.com/csharp/barcode/”, “visual-studio-logo.png”, 500);
  3. MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
  4. //Save as PDF
  5. MyQRWithLogo.SaveAsPdf(“MyQRWithLogo.pdf”);
  6. //Also Save as HTML
  7. MyQRWithLogo.SaveAsHtmlFile(“MyQRWithLogo.html”);

Copy code to clipboardVB  C#

Verifying QR Codes

When adding logos to QR codes and changing colors, we want to make sure that our QR is still readable.

By using the GeneratedBarcode.Verify() method, we can test whether or not our barcode is still readable.

In the following code example, we will see that changing a QR code to light blue actually makes it unreadable. We detect this within the code and prefer dark blue.

  1. // Verifying QR Codes
  2. // using System.Drawing;
  3. var MyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo(“https://ironsoftware.com/csharp/barcode/”, “visual-studio-logo.png”, 350);
  4. MyVerifiedQR.ChangeBarCodeColor(Color.LightBlue);
  5. if (!MyVerifiedQR.Verify())
  6. {
  7. Console.WriteLine(“t LightBlue is not dark enough to be read accurately. Lets try DarkBlue”);
  8. MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);
  9. }
  10. MyVerifiedQR.SaveAsHtmlFile(“MyVerifiedQR.html”);
  11. // open the barcode htm, file in your default web browser
  12. System.Diagnostics.Process.Start(“MyVerifiedQR.html”);

Copy code to clipboardVB  C#

The final lines of code export the QR code as a standalone HTML file with no assets and then open that HTML file in your default browser.

C# read and write QR

Reading and Writing Binary Data

QR codes are an excellent format for dealing with binary data. Sometimes binary data is simply more space-efficient or more appropriate than dealing with text.

In this example, we will encode some binary data from a string, write that to a barcode in QR format, and then read that back as a bit array of binary data. You will note that this feature is not common to many barcode libraries, making Iron Barcode unique in this capacity.

  1. // Reading and Writing Binary Data
  2. // using System.Linq;
  3. //Create Some Binary Data – This example equally well for Byte[] and System.IO.Stream
  4. byte[] BinaryData = System.Text.Encoding.UTF8.GetBytes(“https://ironsoftware.com/csharp/barcode/”);
  5. //WRITE QR with Binary Content
  6. QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage(“MyBinaryQR.png”);
  7. //READ QR with Binary Content
  8. var MyReturnedData = BarcodeReader.QuicklyReadOneBarcode(“MyBinaryQR.png”);
  9. if (BinaryData.SequenceEqual(MyReturnedData.BinaryValue))
  10. {
  11. Console.WriteLine(“t Binary Data Read and Written Perfectly”);
  12. }
  13. else
  14. {
  15. throw new Exception(“Corrupted Data”);
  16. }

Copy code to clipboardVB  C#C# read and write binary data as a QR Code

In summary, the Barcode C# Library is specifically designed with real world QR code usage in mind. Not only does it read QR codes quickly and accurately, but it allows us to style them, add logos, verify barcodes, and encode them with binary data.

Reading QR Codes

To save you jumping between tutorials I will add a code sample for my favorite way to read QR codes using Iron BarCode.

  1. using IronBarCode;
  2. using System;
  3. using System.Drawing;
  4. //…
  5. BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode(“QR.png”, BarcodeEncoding.QRCode);
  6. if (Result != null)
  7. {
  8. Console.WriteLine(QRResult.Value);
  9. }

Copy code to clipboardVB  C#

A more advanced form also exists with more developer control:

  1. using IronBarCode;
  2. using System;
  3. using System.Drawing;
  4. //…
  5. BarcodeResult Result = BarcodeReader.ReadASingleBarcode(“QR.png”, BarcodeEncoding.QRCode, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None);
  6. if (Result != null)
  7. {
  8. Console.WriteLine(QRResult.Value);
  9. }

Copy code to clipboardVB  C#

Moving Forward

To learn more about this example and working with QR codes in C#, we encourage you to fork it on our GitHub page or download the source code from our site.

Source Code Downloads

Download this C# QR Code Generator Tutorial and DLL.

Further Documentation

You may also find the QRCodeWriter and BarcodeReader classes within the Object Reference of interest.

They document the full feature set of Iron Barcode, a VB.NET Barcode Generator, which could not possibly be packed into a single tutorial.

Data & News supplied by www.cloudquote.io
Stock quotes supplied by Barchart
Quotes delayed at least 20 minutes.
By accessing this page, you agree to the following
Privacy Policy and Terms and Conditions.