Generate Barcode in asp.net MVC

Generate Barcode in asp.net MVC (display as image in html)
Barcode can be generated very easily using zxing. Zxing is open source library which supports decoding and generating of barcodes (like QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within images.

The project is a port of the java based barcode reader and generator library ZXing.
You can find the project herehttps://github.com/zxing/zxing
There is a mirror of the repository at github:
https://github.com/micjahn/ZXing.Net
The following barcodes are supported by the decoder:
UPC-A, UPC-E, EAN-8, EAN-13, Code 39, Code 93, Code 128, ITF, Codabar, MSI, RSS-14 (all variants), QR Code, Data Matrix, Aztec and PDF-417.
The encoder supports the following formats:
UPC-A, EAN-8, EAN-13, Code 39, Code 128, ITF, Codabar, Plessey, MSI, QR Code, PDF-417, Aztec, Data Matrix

Steps for generating barcode.
  • Create a new MVC Project
  • Right click on project in solution explorer and click. Manage nuget Packages
  • click on browse tab. Search for zxing.net
  • Click on install button on right side
OR you can use the following command in package manager console to install zxing.net in your project

PM> Install-Package ZXing.Net -Version 0.16.4
Add the following Action Methods to your controller.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using MVC_CRUD_ADO.DAL;
using MVC_CRUD_ADO.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXing;
namespace MVC_CRUD_ADO.Controllers
{
    public class UsersController : Controller
    {
        
        public ActionResult DisplayBarcode()
        {
            return View();
        }

        public ActionResult RenderBarcode(string userid)
        {
            Image img = null;
            using (var ms = new MemoryStream())
            {
                var writer = new ZXing.BarcodeWriter() { Format = BarcodeFormat.CODE_128 };
                writer.Options.Height = 80;
                writer.Options.Width = 280;
                writer.Options.PureBarcode = true;
                img = writer.Write(userid);
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return File(ms.ToArray(), "image/jpeg");
            }
        }
}


View (DisplayBarcode.cshtml) should be like the following
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
@{
    ViewBag.Title = "DisplayBarcode";
}
<hr />
<div class="form-horizontal">
    <div class="row">
        <div class="col-md-8">
            <img src="@Url.Action("RenderBarcode", "Users",new {userid="accus-5689"})" /><hr/>
           
        </div>
    </div>
    <div class="row">
        <div class="col-md-8">
            <img src="@Url.Action("RenderBarcode", "Users",new {userid="7987977987"})" width="450" height="80" />
        </div>
    </div>
</div>

4 Comments

  1. Dear sir;
    Due you respectfully I beg to say that. I hope you are well. sir how can we travel table data from Ms sql server through barcode into ASP.Net MVC or C# window form.

    ReplyDelete
  2. how to show text in the bottom of barcode (Zxing)

    ReplyDelete
  3. var writer = new ZXing.BarcodeWriter() { Format = BarcodeFormat.CODE_128 };

    show error at BarcodeWriter

    please help me

    ReplyDelete

Post a Comment