How to Create Barcodes in ASP .NET with IronBarcode

In this tutorial, we explore how to create barcodes in ASP.NET using Iron Barcode and Visual Studio 2022. Start by installing Iron Barcode through the NuGet Package Manager, ensuring you have the latest version for optimal performance. The process involves editing the index.cshtml.cs file found in the page folder, where pre-written code generates a QR code with a URL and text annotations. Customize the barcode’s appearance by changing its color to green and adjusting margins before saving it as a PNG file in the wwwroot directory. An HTML form with a submit button is added to the index.cshtml file, triggering the barcode generation upon submission. The barcode image is then displayed using an IMG tag. By running the project and clicking the 'Create Barcode' button, the barcode is generated with the predefined settings. This tutorial provides a comprehensive guide to creating barcodes, offering valuable insights into customization and code integration in ASP.NET.

Here is a sample code snippet for generating a QR code in the index.cshtml.cs file:

using IronBarCode; // Import the Iron Barcode library

public class IndexModel : PageModel
{
    public void OnPost()
    {
        // Define the content of the QR code
        string qrContent = "https://example.com";

        // Generate a QR code from the content
        var qrCode = QRCodeWriter.CreateQrCode(qrContent, 300);

        // Customize the QR code's color to green and adjust the margin
        qrCode.ChangeBarCodeColor(System.Drawing.Color.Green);

        // Specify the path where the image will be saved
        string filePath = "wwwroot/qrcode.png";

        // Save the QR code as a PNG file
        qrCode.SaveAsPng(filePath);
    }
}
using IronBarCode; // Import the Iron Barcode library

public class IndexModel : PageModel
{
    public void OnPost()
    {
        // Define the content of the QR code
        string qrContent = "https://example.com";

        // Generate a QR code from the content
        var qrCode = QRCodeWriter.CreateQrCode(qrContent, 300);

        // Customize the QR code's color to green and adjust the margin
        qrCode.ChangeBarCodeColor(System.Drawing.Color.Green);

        // Specify the path where the image will be saved
        string filePath = "wwwroot/qrcode.png";

        // Save the QR code as a PNG file
        qrCode.SaveAsPng(filePath);
    }
}
Imports IronBarCode ' Import the Iron Barcode library

Public Class IndexModel
	Inherits PageModel

	Public Sub OnPost()
		' Define the content of the QR code
		Dim qrContent As String = "https://example.com"

		' Generate a QR code from the content
		Dim qrCode = QRCodeWriter.CreateQrCode(qrContent, 300)

		' Customize the QR code's color to green and adjust the margin
		qrCode.ChangeBarCodeColor(System.Drawing.Color.Green)

		' Specify the path where the image will be saved
		Dim filePath As String = "wwwroot/qrcode.png"

		' Save the QR code as a PNG file
		qrCode.SaveAsPng(filePath)
	End Sub
End Class
$vbLabelText   $csharpLabel

In the index.cshtml file, add the following HTML to create a form with a submit button. This form triggers the barcode generation:

<form method="post">
    <button type="submit">Create Barcode</button>
</form>

<!-- Display the generated barcode image -->
<img src="/qrcode.png" alt="QR Code" />
<form method="post">
    <button type="submit">Create Barcode</button>
</form>

<!-- Display the generated barcode image -->
<img src="/qrcode.png" alt="QR Code" />
HTML

Further Reading: How to Create Barcodes in ASP .NET with IronBarcode

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Generate a QR Code Using C# with IronBarcode
NEXT >
A comparison of IronBarcode and the ZXing Net library