How To Generate Barcode In C#

In this article, we will learn how to generate a barcode in C#.

A Barcode is a method of representing data in a visual, machine-readable form.

The Zen.Barcode.Rendering.Framework is a .NET library for generating Barcodes. Extremely fast, flexible, and easy to use.

Install Zen Barcode Rendering Framework, to install this you need to execute below command in Package Manager Console or find Zen.Barcode.Rendering.Framework in Nuget-Solution.

Package Manager Console

PM> Install-Package Zen.Barcode.Rendering.Framework

.NET CLI Console

> dotnet add package Zen.Barcode.Rendering.Framework

Also, we must have to install System.Drawing.Common package to generate a barcode as an Image.

Package Manager Console

PM> Install-Package System.Drawing.Common

.NET CLI Console

> dotnet add package System.Drawing.Common

Firstly we design a form with some input fields for accepting data to generate barcode.

Open the Index.cshtml file and add the code in it.

<form method="post" class="form-inline">
    <input class="form-control mr-2" name="txtBarcode" required />
    <button class="btn btn-primary">GENERATE</button>
</form>
<br />
<img src="@ViewBag.BarcodeImage" />

Generating a Barcode – Without Text

Open the HomeController.cs file and add the code in it.

using System;
using Microsoft.AspNetCore.Mvc;
using Zen.Barcode;
using System.IO;
using System.Drawing.Imaging;

namespace Generate_Barcode.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string txtBarcode)
        {
            if (!string.IsNullOrEmpty(txtBarcode))
            {
                var barcodeImage = BarcodeDrawFactory.Code128WithChecksum.Draw(txtBarcode, 50);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    barcodeImage.Save(memoryStream, ImageFormat.Png);
                    ViewBag.BarcodeImage = "data:image/png;base64," + Convert.ToBase64String(memoryStream.ToArray());
                }
            }
            return View();
        }
    }
}

Output:

 

Generating a Barcode – With Text

Open the HomeController.cs file and add the code in it.

using System;
using Microsoft.AspNetCore.Mvc;
using Zen.Barcode;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;

namespace Generate_Barcode.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string txtBarcode)
        {
            if (!string.IsNullOrEmpty(txtBarcode))
            {
                var barcodeImage = BarcodeDrawFactory.Code128WithChecksum.Draw(txtBarcode, 50);
                var resultImage = new Bitmap(barcodeImage.Width, barcodeImage.Height + 20); // 20 is bottom padding, to adjust text.
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (Graphics graphics = Graphics.FromImage(resultImage))
                    {
                        Font font = new Font("Arial", 10);
                        SolidBrush brush = new SolidBrush(Color.Black);
                        PointF point = new PointF(25f, 50f);
                        graphics.Clear(Color.White);
                        graphics.DrawImage(barcodeImage, 0, 0);
                        graphics.DrawString(txtBarcode, font, brush, point);
                    }
                    resultImage.Save(memoryStream, ImageFormat.Png);
                    ViewBag.BarcodeImage = "data:image/png;base64," + Convert.ToBase64String(memoryStream.ToArray());
                }
            }
            return View();
        }
    }
}

Output:

 

Please give your valuable feedback and if you have any questions or issues about this article, please let me know.

Also, check How To Generate QR Code In C#

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories