Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
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" />
Further Reading: How to Create Barcodes in ASP .NET with IronBarcode