DEV Community

Cover image for How to Read QR Code in C# (Step-by-Step Tutorial)
Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on

How to Read QR Code in C# (Step-by-Step Tutorial)

QR codes (Quick Response Code) have become important in our digital landscape, seamlessly integrating into various aspects of our lives, from marketing to logistics, and beyond. Their ability to store information in a compact, easily scannable format makes them invaluable in modern applications. However, unlocking this potential often requires robust tools and libraries. In C#, IronQR stands out as a powerful solution for reading QR codes efficiently and effectively. In this article, we will learn QR code reading in C# using IronQR, exploring its features, implementation, and real-world applications.

How to Read a QR Code in C#:

  1. Begin by installing the IronQR library in your project via NuGet.
  2. Load the QR code image into an AnyBitmap object.
  3. Instantiate a QrReader object to handle the QR code reading process.
  4. Use the QrReader object to read the image and store its values in an enumerable collection of QrResult objects.
  5. Iterate through the QrResult collection and print the values to the screen.

Understanding IronQR

IronQR is a C# library designed to read QR codes with ease and precision. IronQR provides developers with a comprehensive set of classes to integrate QR code reading capabilities into their C# applications seamlessly. It provides methods to create and read QR codes. Whether you're building a mobile app, a web service, or a desktop application, IronQR's intuitive API and robust feature set make it a valuable asset in your development toolkit.

Features of IronQR

High-Speed Reading: IronQR leverages advanced algorithms to decode QR codes swiftly, ensuring minimal latency in scanning operations.

Support for Various Formats: Whether it's a URL, text, contact information, or Wi-Fi credentials, IronQR can decode QR codes containing diverse types of data.

Error Correction: IronQR incorporates error correction mechanisms to enhance the reliability of QR code decoding, even in the presence of damaged or obscured codes.

Platform Compatibility: IronQR is designed to seamlessly integrate with C# applications targeting a wide range of platforms, including desktop, web, and mobile environments.

Ease of Use: With a clean and intuitive API, IronQR simplifies the process of QR code integration, allowing developers to focus on building innovative applications without the hassle of low-level code.

Getting Started with Reading QR Code:

We need to install IronQR Library into our project to read QR Code data from QR code image.

Install IronQR NuGet Package:

You can install the IronQR library by running the following command in the Package Manager Console.

install-Package IronQR
Enter fullscreen mode Exit fullscreen mode

The above command will install IronQR with all its dependencies.
Install Nuget Package

C# QR Code Reader:

Now, let's write a C# code example to scan QR code image and read its value. This code will show how to load a QR code image, decode it, and extract the embedded information using the IronQR library.

  static void Main(string[] args)
        {
            var inputBmp = AnyBitmap.FromFile("qr_code.png");

            // Load the asset into QrImageInput
            QrImageInput imageInput = new QrImageInput(inputBmp);

            // Create a QR Reader object
            QrReader reader = new QrReader();

            // Read the Input an get all embedded QR Codes
            IEnumerable<QrResult> results = reader.Read(imageInput);

            foreach (QrResult result in results)
            {
                Console.WriteLine(result.Url);
            }
Enter fullscreen mode Exit fullscreen mode

The above C# code demonstrates how to read QR codes from an image file using the IronQR library. It starts by loading the image "qr_code.png" into an AnyBitmap object. This image is then encapsulated in a QrImageInput object, which is used as input for the QR code reader. A QrReader object is created to facilitate the reading process. The Read method of the QrReader is called with the QrImageInput, returning a collection of QrResult objects, each representing a detected QR code.

The code iterates over these results and prints the URL contained in each QR code to the console. This example showcases how to integrate QR code reading functionality into your C# application effectively. In this way, we can scan QR Codes and read their value.

The Output is as:
C# QR Code Reader

Creating QR codes in C#:

Let's write a C# code example to create a QR code. This code will demonstrate how to generate a QR code, customize it with specific options, and save it as an image file using the IronQR library.

 // Create a QR Code object
 QrCode myQr = QrWriter.Write("https://en.wikipedia.org/wiki/QR_code");

 // Save QR Code as a Bitmap
 AnyBitmap qrImage = myQr.Save();

 // Save QR Code Bitmap as File
 qrImage.SaveAs("qr)code.png");
Enter fullscreen mode Exit fullscreen mode

This above C# code demonstrates how to generate and save a QR code using the IronQR library. First, it creates a QrCode object containing a URL ("https://en.wikipedia.org/wiki/QR_code") using the QrWriter.Write method. The QR code is then saved as a bitmap image in an AnyBitmap object. Finally, the bitmap is saved to a file named "qr_code.png". This is useful for generating QR codes for URLs, or any other data which can then be shared or printed for easy access to web links.
QR Code

Generate QR Code with Logo:

Let's add a logo and some style to our QR code to make it more visually appealing and on-brand. By customizing the QR code with specific dimensions, margins, and an embedded logo, we can enhance its appearance while maintaining its functionality. Here’s an example of how to achieve this using IronQR.

static void Main(string[] args)
{

    // Set QR options
    QrOptions options = new QrOptions(IronQr.QrErrorCorrectionLevel.High, 20);

    // Create a QR Code object
    QrCode myQr = QrWriter.Write("https://en.wikipedia.org/wiki/QR_code", options);

    // Fancy style options
    AnyBitmap logoBmp = new AnyBitmap("logo.png");

    QrStyleOptions style = new QrStyleOptions
    {
        Dimensions = 300, // px
        Margins = 10, // px
        Logo = new QrLogo
        {
            Bitmap = logoBmp,
            Width = 200,
            Height = 200,
            CornerRadius = 2
        }
    };

    // Save QR Code as a Bitmap
    AnyBitmap qrImage = myQr.Save(style);

    // Save QR Code Bitmap as File
    qrImage.SaveAs("qr_styled.png");
}
Enter fullscreen mode Exit fullscreen mode

The above C# code snippet shows how to generate a styled QR code with the IronQR library. It begins by setting QR options, specifying a high error correction level, and a module size of 20. A QR code object is created for the URL "https://en.wikipedia.org/wiki/QR_code" using these options. Additionally, a logo is loaded from "logo.png" and incorporated into the QR code with specific style options, including dimensions, margins, and logo size. The QR code is then saved as a bitmap using these style settings and saved to a file named "qr_styled.png".

This approach is useful for creating visually appealing QR codes for marketing materials, ensuring they are both functional and aesthetically aligned with branding.

QR Code with Logo

Conclusion:

In summary, integrating QR code reading and generation into C# applications is streamlined and efficient with IronQR, one of the best QR code libraries available. By utilizing IronQR in Visual Studio, developers can effortlessly manage QR codes in various formats like PNG. The examples provided demonstrate how to read and generate QR codes, customize them with logos, and implement them in real-world applications, showcasing the versatility of IronQR across .NET Core and .NET Standard platforms.

Whether you're working on a mobile app, a web service, or a desktop application, the source code examples offer a practical starting point. IronQR's high performance, ease of use, and comprehensive feature set make it a valuable tool for any developer.

For those interested, IronQR offers a free trial to explore its capabilities further. If you find it beneficial, you can obtain a commercial license to continue leveraging its full potential in your projects. This ensures you have a reliable, powerful solution for all your QR code needs, backed by robust support and continuous updates.

Top comments (0)