USING IRONQR

How to Generate QR Codes in ASP .NET Core

Published June 6, 2024
Share:

Introduction

QR codes have become an integral part of modern technology, offering a convenient way to store and transmit information. In web development, QR code generators like the one we'll implement using IronQR in ASP.NET Core, provide robust capabilities for dynamic QR code creation. QR code generators are invaluable for various applications such as ticketing systems, authentication, inventory management, and more. This article delves into the process of creating QR codes in ASP.NET Core using IronQR, a powerful library designed for this purpose. ASP.NET Core, being a versatile framework for building web applications, offers robust capabilities for generating QR codes. In this article, we'll delve into the process of creating QR codes in ASP.NET Core using IronQR, a powerful library for QR code generation from Iron Software.

How to Generate QR Codes in ASP.NET Core

  1. Create ASP.Net project using Visual Studio
  2. Install IronQR library from Package Manager
  3. Generate QR Code
  4. Generate QR Code with Formatting

Understanding IronQR

IronQR is a high-performance QR code generation library for .NET applications. It provides a simple and intuitive API for generating QR codes with various customization options. IronQR supports .NET Standard, making it compatible with a wide range of platforms, including ASP.NET Core. With IronQR, developers can effortlessly generate QR codes with different data types, error correction levels, sizes, and formats.

Key features

1. Reading and Creating QR Codes

IronQR enables you to generate and read QR codes with ease. Whether you need to create QR codes dynamically or extract information from existing ones, this library has you covered.

2. User-Friendly API

The library provides a user-friendly API that allows developers to integrate barcode functionality into their .NET projects quickly. You can start working with QR codes in just a few minutes.

3. Compatibility

IronQR supports various .NET versions, including:

  1. C#, VB.NET, F#
  2. .NET Core (8, 7, 6, 5, and 3.1+)
  3. .NET Standard (2.0+)
  4. .NET Framework (4.6.2+)

It covers a wide range of project types, including web (Blazor & WebForms), mobile (Xamarin & MAUI), desktop (WPF & MAUI), and console applications.

4. Machine Learning Model for QR Detection

IronQR uses an advanced custom Machine Learning model to detect QR codes. This ensures accurate and reliable code recognition. Additionally, there’s a Slim Mode option (non-ML) available for those who prefer a lightweight approach.

5. Reading QR Codes

You can read QR codes from various image formats, including:

  1. Images (jpg, png, svg, bmp)
  2. Multipage Images (gif, tif, tiff)
  3. System.Drawing Bitmaps
  4. IronDrawing Images (AnyBitmap)

6. Writing QR Codes

IronQR allows you to write QR codes for different document types, such as:

  1. Images (jpg, png, gif, tiff, bmp)
  2. System.Drawing Images
  3. Streams (MemoryStream, byte[])
  4. PDF (Stamp on Existing PDF)

7. Styling QR Codes

Customize QR codes by Resizing, Adjusting margins and borders, Recoloring, Adding logos

8. Error Handling and Correction

IronQR provides detailed error messages and supports custom QR error correction levels.

With this knowledge, let's get started with the application to generate QR code in ASP.NET Core.

Step 1: Create a new ASP.NET project using Visual Studio

Before we dive into QR code generation, let's set up a new ASP.NET Core project. Here are the steps to initiate a new project:

Start by creating a new project in Visual Studio and selecting ASP.Net Core Web App application template

How to Generate QR Codes in ASP .NET Core: Figure 1 - Select the ASP.NET CORE web application template

Provide Project Name and location

How to Generate QR Codes in ASP .NET Core: Figure 2 - Name the project and the location you wish to save to

Select the required .NET version.

How to Generate QR Codes in ASP .NET Core: Figure 3 - Select the correct .NET version.

Click the "create" button to generate the application code from the template

How to Generate QR Codes in ASP .NET Core: Figure 4 - Click the "create" button to generate the template code

Step 2: Install IronQR library from the Package Manager

Install IronQR library from Visual Studio Package Manager as shown below.

How to Generate QR Codes in ASP .NET Core: Figure 5 - Search IronOCR using Visual Studio Package Manager and install it

IronQR can also be installed using NuGet Package Manager.

How to Generate QR Codes in ASP .NET Core: Figure 6 - Search for IronOCR using NuGet Package Manager

Step 3: Generate a QR Code

Now, let's create a QR code app.

Create QRCode Model

using System.ComponentModel.DataAnnotations;
namespace GenerateQRCode.Models
{
    public class QRCodeModel
    {
        [Display(Name = "Enter QR Code Text")]
        public string QRCodeText
        {
            get;
            set;
        }
    }
}
using System.ComponentModel.DataAnnotations;
namespace GenerateQRCode.Models
{
    public class QRCodeModel
    {
        [Display(Name = "Enter QR Code Text")]
        public string QRCodeText
        {
            get;
            set;
        }
    }
}
Imports System.ComponentModel.DataAnnotations
Namespace GenerateQRCode.Models
	Public Class QRCodeModel
		<Display(Name := "Enter QR Code Text")>
		Public Property QRCodeText() As String
	End Class
End Namespace
VB   C#

Create a QR Code Controller

To add a new controller, right-click on the controller folder and provide a name as shown below.

How to Generate QR Codes in ASP .NET Core: Figure 7 - Add a new controller by clicking on the folder and give it a name

Select the Empty Controller.

How to Generate QR Codes in ASP .NET Core: Figure 8 - Click Empty Controller on the prompt

Provide a name.

How to Generate QR Codes in ASP .NET Core: Figure 9 - Name the controller

Next, integrate the following code into the controller.

using GenerateQRCode.Models;
using IronQr;
using IronSoftware.Drawing;
using Microsoft.AspNetCore.Mvc;
namespace GenerateQRCode.Controllers
{
    public class QrCodeController : Controller
    {
        private readonly IWebHostEnvironment _environment;
        public QrCodeController(IWebHostEnvironment environment)
        {
            _environment = environment;
        }
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult CreateQRCode(QRCodeModel generateQRCode)
        {
            try
            {
                string path = Path.Combine(_environment.WebRootPath, "GeneratedQRCode");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string filePath = Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png");
                // Create a QR Code object
                QrCode myQr = QrWriter.Write(generateQRCode.QRCodeText);
                // Save QR Code as a Bitmap
                AnyBitmap qrImage = myQr.Save();
                // Save QR Code Bitmap as File
                qrImage.SaveAs(filePath);
                string fileName = Path.GetFileName(filePath); // qr code file
                string imageUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}" + "/GeneratedQRCode/" + fileName;
                ViewBag.QrCodeUri = imageUrl;
            }
            catch (Exception)
            {
                throw;
            }
            return View();
        }
    }
}
using GenerateQRCode.Models;
using IronQr;
using IronSoftware.Drawing;
using Microsoft.AspNetCore.Mvc;
namespace GenerateQRCode.Controllers
{
    public class QrCodeController : Controller
    {
        private readonly IWebHostEnvironment _environment;
        public QrCodeController(IWebHostEnvironment environment)
        {
            _environment = environment;
        }
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult CreateQRCode(QRCodeModel generateQRCode)
        {
            try
            {
                string path = Path.Combine(_environment.WebRootPath, "GeneratedQRCode");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string filePath = Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png");
                // Create a QR Code object
                QrCode myQr = QrWriter.Write(generateQRCode.QRCodeText);
                // Save QR Code as a Bitmap
                AnyBitmap qrImage = myQr.Save();
                // Save QR Code Bitmap as File
                qrImage.SaveAs(filePath);
                string fileName = Path.GetFileName(filePath); // qr code file
                string imageUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}" + "/GeneratedQRCode/" + fileName;
                ViewBag.QrCodeUri = imageUrl;
            }
            catch (Exception)
            {
                throw;
            }
            return View();
        }
    }
}
Imports GenerateQRCode.Models
Imports IronQr
Imports IronSoftware.Drawing
Imports Microsoft.AspNetCore.Mvc
Namespace GenerateQRCode.Controllers
	Public Class QrCodeController
		Inherits Controller

		Private ReadOnly _environment As IWebHostEnvironment
		Public Sub New(ByVal environment As IWebHostEnvironment)
			_environment = environment
		End Sub
		Public Function Index() As IActionResult
			Return View()
		End Function
		<HttpPost>
		Public Function CreateQRCode(ByVal generateQRCode As QRCodeModel) As IActionResult
			Try
				Dim path As String = System.IO.Path.Combine(_environment.WebRootPath, "GeneratedQRCode")
				If Not Directory.Exists(path) Then
					Directory.CreateDirectory(path)
				End If
				Dim filePath As String = System.IO.Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png")
				' Create a QR Code object
				Dim myQr As QrCode = QrWriter.Write(generateQRCode.QRCodeText)
				' Save QR Code as a Bitmap
				Dim qrImage As AnyBitmap = myQr.Save()
				' Save QR Code Bitmap as File
				qrImage.SaveAs(filePath)
				Dim fileName As String = System.IO.Path.GetFileName(filePath) ' qr code file
				Dim imageUrl As String = $"{Me.Request.Scheme}://{Me.Request.Host}{Me.Request.PathBase}" & "/GeneratedQRCode/" & fileName
				ViewBag.QrCodeUri = imageUrl
			Catch e1 As Exception
				Throw
			End Try
			Return View()
		End Function
	End Class
End Namespace
VB   C#

Code Explanation

Namespace and Class Definition

The code starts with using statements, which import necessary namespaces. The GenerateQRCode.Controllers namespace contains the QrCodeController class.

Controller Constructor

The QrCodeController class has a constructor that takes a IWebHostEnvironment parameter. This parameter is injected by ASP.NET Core for handling web hosting-related tasks.

Action Methods

Index(): This method returns a view (presumably an HTML page) when accessed. It doesn’t seem to be directly related to QR code generation. CreateQRCode(QRCodeModel generateQRCode): This method is the heart of the QR code generation logic. It receives a QRCodeModel object (presumably containing the QR code text) as a parameter.

Inside the method

It constructs a path for saving the generated QR code image. Creates a QR code object using QrWriter.Write(generateQRCode.QRCodeText). Saves the QR code as a bitmap image. Constructs an image URL based on the web request details. Sets the ViewBag.QrCodeUri property with the image URL. If any exceptions occur during this process, they are thrown.

QR Code Generation

The actual QR code generation happens within the CreateQRCode method. The QrWriter.Write(generateQRCode.QRCodeText) call creates a QR code object based on the provided text.

Image Saving

The generated QR code is saved as a bitmap image using qrImage.SaveAs(filePath). The image file path is constructed based on the web root path and the desired file name.

Image URL

The imageUrl is constructed using the web request’s scheme, host, and path base. This URL points to the location where the generated QR code image can be accessed. Overall, this controller handles QR code generation, saving the image, and providing an image URL for further use

Add View To Controller

To add a new view, right-click the CreateQRCode action method in the QrCodeController class.

How to Generate QR Codes in ASP .NET Core: Figure 10 - Add a new view in the `QrCodeController` class

Select "Add View" then select "Razor View". Click on the "Add" button.

How to Generate QR Codes in ASP .NET Core: Figure 11 - Select the Add View option, then Razor View, then Add

Then select the "Create" template and Model class generated previously.

How to Generate QR Codes in ASP .NET Core: Figure 12 - Select the `Create` template generated previously

Now replace the below code in the view

@model GenerateQRCode.Models.QRCodeModel
@{
    ViewData["Title"] = "Generate QR Code";
}
<h1>Generate QR Code</h1>
<h4>QRCodeModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="CreateQRCode">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="QRCodeText" class="control-label"></label>
                <span asp-validation-for="QRCodeText" class="text-danger"></span>
            </div>
            <div class="form-group">
            </div>
            <div class="form-group">
                <img src="@ViewBag.QrCodeUri" class="img-thumbnail" />
            </div>
        </form>
    </div>
</div>
<div>
    <a asp-action="Index">Clear</a>
</div>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
@model GenerateQRCode.Models.QRCodeModel
@{
    ViewData["Title"] = "Generate QR Code";
}
<h1>Generate QR Code</h1>
<h4>QRCodeModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="CreateQRCode">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="QRCodeText" class="control-label"></label>
                <span asp-validation-for="QRCodeText" class="text-danger"></span>
            </div>
            <div class="form-group">
            </div>
            <div class="form-group">
                <img src="@ViewBag.QrCodeUri" class="img-thumbnail" />
            </div>
        </form>
    </div>
</div>
<div>
    <a asp-action="Index">Clear</a>
</div>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
model ReadOnly Property () As GenerateQRCode.Models.QRCodeModel
	ViewData("Title") = "Generate QR Code"
End Property
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> </div> <div class="form-group"> <img src="@ViewBag.QrCodeUri" class="img-thumbnail" /> </div> </form> </div> </div> <div> <a asp-action="Index"> Clear</a> </div> @section Scripts
"img-thumbnail" /> </div> </form> </div> </div> (Of div) <a asp-action="Index"> Clear</a> </div> section Scripts
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> </div> <div class="form-group"> <img src="@ViewBag.QrCodeUri" class="img-thumbnail" /> </div> </form> </div> </div> <div> <a asp-action
"@ViewBag.QrCodeUri" class="img-thumbnail" /> </div> </form> </div> </div> (Of div) <a asp-action
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> </div> <div class="form-group"> <img src="@ViewBag.QrCodeUri" class
"form-group"> <img src="@ViewBag.QrCodeUri" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> </div> <div class="form-group"> <img src
"form-group"> </div> <div class="form-group"> <img src
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> </div> <div class
"text-danger"></span> </div> <div class="form-group"> </div> <div class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class
"QRCodeText" class="text-danger"></span> </div> <div class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <span asp-validation-for="QRCodeText" class
"control-label"></label> <span asp-validation-for="QRCodeText" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <span asp-validation-for
"QRCodeText" class="control-label"></label> <span asp-validation-for
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class
"form-group"> <label asp-for="QRCodeText" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for
"text-danger"></div> <div class="form-group"> <label asp-for
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class
"ModelOnly" class="text-danger"></div> <div class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class
"CreateQRCode"> <div asp-validation-summary="ModelOnly" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary
"col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Private Private Friend <h1> Generate QR Code</h1> <h4> QRCodeModel</h4> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action
"row"> <div class="col-md-4"> <form asp-action
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Private Private Private Private Private Private Private Private Private Private Private Private Private Private Friend (Of h1) Generate QR Code</h1> (Of h4) QRCodeModel</h4> <hr /> <div Class="row"> <div class
	@
	If True Then
		Await Html.RenderPartialAsync("_ValidationScriptsPartial")
	End If
End Class
VB   C#

Do the same for the index action method also, so that when the app is launched, it will not throw an error for the POST request.

Now in the program.cs change the following code to make the above view as default route.

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=QrCode}/{action=Index}"
);
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=QrCode}/{action=Index}"
);
app.MapControllerRoute(name:= "default", pattern:= "{controller=QrCode}/{action=Index}")
VB   C#

This action will modify the default route from HomeController to our QrCode Controller.

Now, Compile and run the project.

How to Generate QR Codes in ASP .NET Core: Figure 13 - Example homepage from the project

Enter any text in the text box and click 'Create.' This will create a new QR code as shown below.

How to Generate QR Codes in ASP .NET Core: Figure 14 - Enter any text and click create to create a new QR code utilizing IronOCR

Adding Style to QR code

QrStyleOptions are used to style the QR code generation.

[HttpPost]
public IActionResult CreateQRCode(QRCodeModel generateQRCode)
{
    try
    {
        string path = Path.Combine(_environment.WebRootPath, "GeneratedQRCode");
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        string filePath = Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png");
        // Create a QR Code obj
        QrCode qrCode = QrWriter.Write(generateQRCode.QRCodeText);
        QrStyleOptions style = new QrStyleOptions
        {
            Dimensions = 300, // px
            Margins = 10, // px
            Color = Color.YellowGreen
        };
        // Save QR Code as a any bitmap
        AnyBitmap qrImage = qrCode.Save(style);
        // Save QR Code Bitmap to File
        qrImage.SaveAs(filePath);
        string fileName = Path.GetFileName(filePath); // qr code file
        string imageUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}" + "/GeneratedQRCode/" + fileName;
        ViewBag.QrCodeUri = imageUrl;
    }
    catch (Exception)
    {
        throw;
    }
    return View();
}
[HttpPost]
public IActionResult CreateQRCode(QRCodeModel generateQRCode)
{
    try
    {
        string path = Path.Combine(_environment.WebRootPath, "GeneratedQRCode");
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        string filePath = Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png");
        // Create a QR Code obj
        QrCode qrCode = QrWriter.Write(generateQRCode.QRCodeText);
        QrStyleOptions style = new QrStyleOptions
        {
            Dimensions = 300, // px
            Margins = 10, // px
            Color = Color.YellowGreen
        };
        // Save QR Code as a any bitmap
        AnyBitmap qrImage = qrCode.Save(style);
        // Save QR Code Bitmap to File
        qrImage.SaveAs(filePath);
        string fileName = Path.GetFileName(filePath); // qr code file
        string imageUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}" + "/GeneratedQRCode/" + fileName;
        ViewBag.QrCodeUri = imageUrl;
    }
    catch (Exception)
    {
        throw;
    }
    return View();
}
<HttpPost>
Public Function CreateQRCode(ByVal generateQRCode As QRCodeModel) As IActionResult
	Try
		Dim path As String = System.IO.Path.Combine(_environment.WebRootPath, "GeneratedQRCode")
		If Not Directory.Exists(path) Then
			Directory.CreateDirectory(path)
		End If
		Dim filePath As String = System.IO.Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png")
		' Create a QR Code obj
		Dim qrCode As QrCode = QrWriter.Write(generateQRCode.QRCodeText)
		Dim style As New QrStyleOptions With {
			.Dimensions = 300,
			.Margins = 10,
			.Color = Color.YellowGreen
		}
		' Save QR Code as a any bitmap
		Dim qrImage As AnyBitmap = qrCode.Save(style)
		' Save QR Code Bitmap to File
		qrImage.SaveAs(filePath)
		Dim fileName As String = System.IO.Path.GetFileName(filePath) ' qr code file
		Dim imageUrl As String = $"{Me.Request.Scheme}://{Me.Request.Host}{Me.Request.PathBase}" & "/GeneratedQRCode/" & fileName
		ViewBag.QrCodeUri = imageUrl
	Catch e1 As Exception
		Throw
	End Try
	Return View()
End Function
VB   C#

Output

How to Generate QR Codes in ASP .NET Core: Figure 15 - Modifying the color of the QR code programmatically

License (Trial Available)

For developers who want to test out IronQR, a trial license is available here. This license key needs to be placed in the appSettings.json file. Doing so will remove the watermark shown in the images above. This will remove the watermark shown in the images above.

{
"IronQR.License.LicenseKey" : "My key"
}

Conclusion

In this article, we've explored how to generate QR codes in ASP.NET Core using IronQR. By leveraging the power of IronQR, developers can easily integrate QR code generation functionality into their web applications. Whether it's for ticketing, authentication, or information sharing, QR codes offer a versatile solution for transmitting data efficiently. With IronQR, creating and customizing QR codes in ASP.NET Core has never been easier. Start incorporating QR code generation into your ASP.NET Core projects and unlock a world of possibilities for dynamic data encoding and sharing.

< PREVIOUS
How to use .NET MAUI For QR Code scanner
NEXT >
How to Scan QR Code in ASP .NET

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 18,287 View Licenses >