Prerequisites
Download and install the following software on your computer
- Visual Studio 2022
- .NET 6.0 SDK
- OnBarcode.Barcode.Common.Skia.dll
Create an ASP.NET Core web app with barcode generation on page using C#
1. Create new ASP.NET Core web app
Start Visual Studio 2022 and select "Create a new project".
Select "ASP.NET Core Web App" in the dialog, and then press "Next" button.
Create a new project with name "OnBarcode.BarcodeGenerator.ASPNETCoreDemo".
Select .NET 6.0 (Long-term support), and then press "Create"
Now, all auto-generated files could be found in the solution explorer.
Open the file "Program.cs" and ensure that it has enabled following Middlewares.
Select "ASP.NET Core Web App" in the dialog, and then press "Next" button.
Create a new project with name "OnBarcode.BarcodeGenerator.ASPNETCoreDemo".
Select .NET 6.0 (Long-term support), and then press "Create"
Now, all auto-generated files could be found in the solution explorer.
Open the file "Program.cs" and ensure that it has enabled following Middlewares.
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapRazorPages(); app.Run();
2. Install SkiaSharp and OnBarcode Barcode Generator dll
Right-click "Dependencies" in the Solution Explorer, and select "Manage NuGet Packages".
Select "Browse" and use the search control to find "SkiaSharp" from the package source "nugget.org".
Choose the latest stable version to install SkiaSharp.
Check "Packages" in the Solution Explorer to ensure the installation is success.
Add DLL reference "OnBarcode.Barcode.Common.Skia.dll".
Select "Browse" and use the search control to find "SkiaSharp" from the package source "nugget.org".
Choose the latest stable version to install SkiaSharp.
Check "Packages" in the Solution Explorer to ensure the installation is success.
Add DLL reference "OnBarcode.Barcode.Common.Skia.dll".
3. Add C# code to generate barcode dynamically on ASP.NET page
Here we will add controls on the web form page and C# code to allow web user enter barcode text massage, and draw barcodes on web page dynamically.
Copy all following codes to replace the content of the file "Index.cshtml".
Copy all following codes to replace the content of the file "Index.cshtml".
@page @model IndexModel @{ ViewData["Title"] = "Home page"; } <div class="text-center"> <form asp-page="./Index" method="post"> <br /> Barcode Message: <input type="text" id="tb1" name="tbMessage" style="width: 600px; " asp-for="@Model.Message" /> <br /> <br /> <input type="submit" value="Create Barcode" /> </form> <br /> <img src="data:image/png;base64,@Model.BarcodeImage" /> </div>
Copy all following codes to replace the content in the file "Index.cshtml.cs".
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace OnBarcode.BarcodeGenerator.ASPNETCoreDemo.Pages { public class IndexModel : PageModel { public String Message { get; set; } = ""; public String BarcodeImage { get; set; } = ""; public void OnGet() { } public Task<IActionResult> OnPost() { this.Message = Request.Form["tbMessage"].ToString(); OnBarcode.Barcode.Linear linear = new OnBarcode.Barcode.Linear(); linear.Type = OnBarcode.Barcode.BarcodeType.CODE128; linear.Data = this.Message; linear.OutputFileFormat = FileFormat.PNG; byte[] dataBytes = linear.drawBarcodeAsBytes(); BarcodeImage = System.Convert.ToBase64String(dataBytes); return Task.FromResult<IActionResult>(Page()); } } }
4. Run web app
It is done. Now press "Ctrl+F5" to run the project.
It launches the Kestrel server and opens the default browser.
More barcode generation functions in C# ASP.NET Core web app tutorial
Here we will demonstrate more C# barcode generating functions in ASP.NET Core web application.
- Create 2D barcode QR Code with Unicode text encoded
- Create and customize generated barcode text (font family, size, color)
- Display or hide printed barcode text
Create 2d barcode with Unicode text encode
By default, the C# barcode library supports 2d barcode (QR Code, Data Matrix and PDF417) creating with ASCII text encode. You can also quickly to enable
Unicode text encoding in 2d barcodes generation in C# ASP.NET Core web application.
In file "Index.cshtml.cs", go to method "OnPost()"
Replace the previous Code 128 barcode generation code with the following C# codes.
In the above C# code, we have enabled the property EncodeUnicodeText to true. The barcode library will process and create QR Code 2d barcode with Unicode text from "Data" property.
Now the web user can input Unicode text on the web browser in ASP.NET Core web application, and create QR Code 2d barcode image on the web page.
In file "Index.cshtml.cs", go to method "OnPost()"
Replace the previous Code 128 barcode generation code with the following C# codes.
QRCode barcode = new QRCode(); barcode.EncodeUnicodeText = true; barcode.Data = this.Message; barcode.OutputFileFormat = FileFormat.PNG; byte[] dataBytes = barcode.drawBarcodeAsBytes(); BarcodeImage = System.Convert.ToBase64String(dataBytes);
In the above C# code, we have enabled the property EncodeUnicodeText to true. The barcode library will process and create QR Code 2d barcode with Unicode text from "Data" property.
Now the web user can input Unicode text on the web browser in ASP.NET Core web application, and create QR Code 2d barcode image on the web page.
Customize created barcode text styles (font, size, color)
In this C# sample codes, we will explain how to create and customize EAN-13 barcode text styles. We have change EAN-13 barcode text font family, font size, and
text color displayed in ASP.NET Core web page.
Here we will create and print EAN-13 barcode on the ASP.NET Core web application. We will also customize the generated EAN-13 barcode text font, color and space between barcode text and barcode modules using C#.
In file "Index.cshtml.cs", go to method "OnPost()"
Replace the existing barcode creating source code with the following C# codes.
In the above C# sample code, we will create and print EAN-13 barcode image on the web page in ASP.NET Core app.
We have also customized the three barcode text related properties
How to create barcode with color, image settings applied using C# Barcode Library?
Here we will create and print EAN-13 barcode on the ASP.NET Core web application. We will also customize the generated EAN-13 barcode text font, color and space between barcode text and barcode modules using C#.
In file "Index.cshtml.cs", go to method "OnPost()"
Replace the existing barcode creating source code with the following C# codes.
Linear barcode = new Linear(); barcode.Type = BarcodeType.EAN13; barcode.Data = this.Message; barcode.TextFont = new SkiaSharp.SKFont( SkiaSharp.SKTypeface.FromFamilyName("OCR-B", SkiaSharp.SKFontStyle.Normal)); barcode.TextColor = new SkiaSharp.SKColor(255, 255, 0, 0); barcode.TextMargin = 20; barcode.OutputFileFormat = FileFormat.PNG; byte[] dataBytes = barcode.drawBarcodeAsBytes(); BarcodeImage = System.Convert.ToBase64String(dataBytes);
In the above C# sample code, we will create and print EAN-13 barcode image on the web page in ASP.NET Core app.
We have also customized the three barcode text related properties
- TextFont Change the default font family to GS1 recommended OCR-B
- TextColor Set the barcode text colors
- TextFont Set the space between EAN-13 bar modules and barcode text
How to create barcode with color, image settings applied using C# Barcode Library?
Display or hide created barcode text
Generating a 1d barcode, you can control and decide how to show or hide the printed barcode text string. The following C# example codes will explain in details
how to render or hide barcode text, show or hide check sum digit, show or hide start/stop characters in Code 39 barcodes in ASP.NET Core tutorial.
In file "Index.cshtml.cs", go to method "OnPost()"
Replace the existing barcode creating source code with the following C# Code 39 generation codes.
The above C# source code will create and display a Code 39 barcode image on the web browser in ASP.NET Core web application. And it also demostrates how to customize the printed Code 39 barcode text
In file "Index.cshtml.cs", go to method "OnPost()"
Replace the existing barcode creating source code with the following C# Code 39 generation codes.
Linear barcode = new Linear(); barcode.Type = BarcodeType.CODE39; barcode.Data = this.Message; barcode.AddCheckSum = true; barcode.ShowCheckSumChar = true; barcode.ShowStartStopInText = true; barcode.ShowText = true; barcode.OutputFileFormat = FileFormat.PNG; byte[] dataBytes = barcode.drawBarcodeAsBytes(); BarcodeImage = System.Convert.ToBase64String(dataBytes);
The above C# source code will create and display a Code 39 barcode image on the web browser in ASP.NET Core web application. And it also demostrates how to customize the printed Code 39 barcode text
- AddCheckSum Check sum character is optional for Code 39. To calculate and append a checksum character on Code 39 barcode, you need enable this property.
- ShowCheckSumChar When check sum is enabled in Code 39 barcode, you can choose displaying or hiding the check digit in printed Code 39 barcode text. If you hide the checksum character, the barcode modules will still calculate and include the check character module.
- ShowStartStopInText Code 39 has two special characters, Start/Stop characters. You can show or hide them through "ShowStartStopInText" option.
- ShowText You can completely display or hide the barcode text using property "ShowText"
Create barcodes through URL in C# ASP.NET Core
The following guide will create an ASP.NET Core Empty for Windows, which allows you to create QR Code and barcodes through URL.
Here are the requirements:
Here are the requirements:
- Visual Studio 2022
- .NET 6.0 SDK
- OnBarcode.Barcode.ASPNETCore.dll for Windows, or OnBarcode.Barcode.ASPNETCore.Skia.dll for Linux, MacOS, non-Windows
- Step 1: Create a new project with project template "ASP.NET Core Empty".
- Step 2: Choose framework .NET 6.0 (Long Term Support).
- Step 3: Add project reference: assembly "OnBarcode.Barcode.ASPNETCore.dll" or "OnBarcode.Barcode.ASPNETCore.Skia.dll" to the project.
- Step 4: For Windows: Install package "System.Drawing.Common" (Version 4.5.0 or later) by NuGet.
For Linux, MacOS, non-Windows: Install package "SkiaSharp" (Version 2.80.0 or later) by NuGet. - Step 5: Replace content in file "Program.cs" by below C# codes
var builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpContextAccessor(); var app = builder.Build(); // Linear app.MapGet("/", async (IHttpContextAccessor httpContextAccessor) => { var context = httpContextAccessor.HttpContext; if (context != null) await OnBarcode.Barcode.ASPNETCore.LinearWebStream.drawBarcodeAsync(context.Request, context.Response); }); // PDF417 app.MapGet("/PDF417", async (IHttpContextAccessor httpContextAccessor) => { var context = httpContextAccessor.HttpContext; if (context != null) await OnBarcode.Barcode.ASPNETCore.PDF417WebStream.drawBarcodeAsync(context.Request, context.Response); }); // DataMatrix app.MapGet("/DataMatrix", async (IHttpContextAccessor httpContextAccessor) => { var context = httpContextAccessor.HttpContext; if (context != null) await OnBarcode.Barcode.ASPNETCore.DataMatrixWebStream.drawBarcodeAsync(context.Request, context.Response); }); // QRCode app.MapGet("/QRCode", async (IHttpContextAccessor httpContextAccessor) => { var context = httpContextAccessor.HttpContext; if (context != null) await OnBarcode.Barcode.ASPNETCore.QRCodeWebStream.drawBarcodeAsync(context.Request, context.Response); }); // MicroPDF417 app.MapGet("/MicroPDF417", async (IHttpContextAccessor httpContextAccessor) => { var context = httpContextAccessor.HttpContext; if (context != null) await OnBarcode.Barcode.ASPNETCore.MicroPDF417WebStream.drawBarcodeAsync(context.Request, context.Response); }); // MicroQRCode app.MapGet("/MicroQRCode", async (IHttpContextAccessor httpContextAccessor) => { var context = httpContextAccessor.HttpContext; if (context != null) await OnBarcode.Barcode.ASPNETCore.MicroQRCodeWebStream.drawBarcodeAsync(context.Request, context.Response); }); app.Run();
- Step 6: Run the demo project (Press Ctrl+F5).
- Step 7: You can send barcode settings through URL as below.
Eg. Default is Linear barcode
https://localhost:7158/?DATA=12345678&X=3
https://localhost:7158/QRCode?DATA=12345678&X=10
https://localhost:7158/MicroPDF417?DATA=123&X=2
Conclusion
Now you have learned how to create and display 2d and linear barcodes on ASP.NET Core Razor page,
and how to generate QR Code and barcodes through URL using MiniAPI.
