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, a powerful library, and Visual Studio 2022. Start by installing Iron Barcode through the NuGet package manager, ensuring you have the latest version for optimal performance. The tutorial demonstrates how to navigate to the Index.cshtml.cs
file under the 'Pages' folder, where pre-written code generates a QR code with a URL and text annotations. Customize your barcode by changing its color to green using the 'change barcode color' method, setting margins, and saving it as a PNG file in the wwwroot
directory. The barcode image path is set to locate this image. An HTML form with a submit button is added to the Index.cshtml file, triggering the barcode generation process upon submission. An IMG tag displays the created barcode. Run the project, press the 'Create Barcode' button, and view the output featuring your customized barcode. This guide concludes with encouragement to subscribe for more tips from Iron Software.
Code Sample for Barcode Generation in ASP.NET:
using IronBarCode; // Import the IronBarcode library
using System;
using System.IO;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace YourNamespace.Pages
{
public class IndexModel : PageModel
{
public string BarcodeImagePath { get; private set; }
// Handles HTTP GET requests
public void OnGet()
{
// Path where the barcode image will be saved
BarcodeImagePath = "/images/barcode.png";
}
// Handles HTTP POST requests
public void OnPost()
{
// Create QR Code with URL and additional text
var QRCode = QRCodeWriter.CreateQrCodeWithLogo("https://example.com", "Your Text");
// Customize the barcode's appearance
QRCode.ChangeBarCodeColor(System.Drawing.Color.Green);
// Define the file path in the wwwroot directory for saving the barcode
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot" + BarcodeImagePath);
QRCode.SaveAsPng(filePath); // Save the barcode as a PNG file
}
}
}
using IronBarCode; // Import the IronBarcode library
using System;
using System.IO;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace YourNamespace.Pages
{
public class IndexModel : PageModel
{
public string BarcodeImagePath { get; private set; }
// Handles HTTP GET requests
public void OnGet()
{
// Path where the barcode image will be saved
BarcodeImagePath = "/images/barcode.png";
}
// Handles HTTP POST requests
public void OnPost()
{
// Create QR Code with URL and additional text
var QRCode = QRCodeWriter.CreateQrCodeWithLogo("https://example.com", "Your Text");
// Customize the barcode's appearance
QRCode.ChangeBarCodeColor(System.Drawing.Color.Green);
// Define the file path in the wwwroot directory for saving the barcode
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot" + BarcodeImagePath);
QRCode.SaveAsPng(filePath); // Save the barcode as a PNG file
}
}
}
Imports IronBarCode ' Import the IronBarcode library
Imports System
Imports System.IO
Imports Microsoft.AspNetCore.Mvc.RazorPages
Namespace YourNamespace.Pages
Public Class IndexModel
Inherits PageModel
Private privateBarcodeImagePath As String
Public Property BarcodeImagePath() As String
Get
Return privateBarcodeImagePath
End Get
Private Set(ByVal value As String)
privateBarcodeImagePath = value
End Set
End Property
' Handles HTTP GET requests
Public Sub OnGet()
' Path where the barcode image will be saved
BarcodeImagePath = "/images/barcode.png"
End Sub
' Handles HTTP POST requests
Public Sub OnPost()
' Create QR Code with URL and additional text
Dim QRCode = QRCodeWriter.CreateQrCodeWithLogo("https://example.com", "Your Text")
' Customize the barcode's appearance
QRCode.ChangeBarCodeColor(System.Drawing.Color.Green)
' Define the file path in the wwwroot directory for saving the barcode
Dim filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot" & BarcodeImagePath)
QRCode.SaveAsPng(filePath) ' Save the barcode as a PNG file
End Sub
End Class
End Namespace
HTML Form for Triggering Barcode Generation:
<!DOCTYPE html>
<html>
<head>
<title>Barcode Generator</title>
</head>
<body>
<form method="POST">
<button type="submit">Create Barcode</button>
</form>
<div>
<img src="@Model.BarcodeImagePath" alt="Generated Barcode" />
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Barcode Generator</title>
</head>
<body>
<form method="POST">
<button type="submit">Create Barcode</button>
</form>
<div>
<img src="@Model.BarcodeImagePath" alt="Generated Barcode" />
</div>
</body>
</html>
Further Reading: How to Create Barcodes in ASP .NET with IronBarcode