Generate Bar Code in MVC Application

It is my first article about to generate bar code on MVC through IDAutomation_Code39FreeFont.

This article explain you how to generate bar code step by step very easily.

Following step follow to generate bar code:

  1. Install IDAutomation_Code39FreeFont on the system which I attached to my article.

  2. Go to control panel -> Font -> and check the IDAutomation_CodeHC39M Free Version (font name) is installed or not ones its installed go to further steps.

  3. File -> New -> Project -> ASP.NET MVC 4 Web Application ->Name: BarCodeGenerators.

  4. In the Controllers folder Just Right Click and Add Controller name as BarCodeController.cs.

  5. Write ActionResult name as BarCode.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.Mvc;  
    6. namespace BarCodeGenerators.Controllers  
    7. {  
    8.     public class BarCodeController: Controller  
    9.     {  
    10.         // GET: /BarCode/  
    11.         public ActionResult BarCode()  
    12.         {  
    13.             return View();  
    14.         }  
    15.     }  
    16. }  
  6. Just right click on ActionResult BarCode and add veiw.

  7. Add class file name as IDAtomationBarCode.cs in the views folder.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Drawing;  
    6. using System.Text;  
    7. using System.IO;  
    8. using System.Drawing.Imaging;  
    9. namespace BarCodeGenerators.Views.BarCode  
    10. {  
    11.     public class IDAtomationBarCode  
    12.     {  
    13.         public static string BarcodeImageGenerator(string Code)  
    14.         {  
    15.             byte[] BarCode;  
    16.             string BarCodeImage;  
    17.             Bitmap objBitmap = new Bitmap(Code.Length * 28, 100);  
    18.             using(Graphics graphic = Graphics.FromImage(objBitmap))  
    19.             {  
    20.                 Font newFont = new Font("IDAutomationHC39M Free Version", 18, FontStyle.Regular);  
    21.                 PointF point = new PointF(2 f, 2 f);  
    22.                 SolidBrush balck = new SolidBrush(Color.Black);  
    23.                 SolidBrush white = new SolidBrush(Color.White);  
    24.                 graphic.FillRectangle(white, 0, 0, objBitmap.Width, objBitmap.Height);  
    25.                 graphic.DrawString("*" + Code + "*", newFont, balck, point);  
    26.             }  
    27.             using(MemoryStream Mmst = new MemoryStream())  
    28.             {  
    29.                 objBitmap.Save(Mmst, ImageFormat.Png);  
    30.                 BarCode = Mmst.GetBuffer();  
    31.                 BarCodeImage = BarCode != null ? "data:image/jpg;base64," + Convert.ToBase64String((byte[]) BarCode) : "";  
    32.                 return BarCodeImage;  
    33.             }  
    34.         }  
    35.     }  
    36. }  
    Note: BarcodeImageGenerator method will generate barcode you can call this method in view and get genrated barcode show on image tag.

  8. Go to the view code and write.
    1. @using BarCodeGenerators.Views.BarCode;  
    2. @{  
    3.     ViewBag.Title = "BarCode";  
    4. }   
    5. < h2 > BarCode < /h2>  
    6. @{  
    7.     var a = IDAtomationBarCode.BarcodeImageGenerator("12345678"); < img src = "@Url.Content(a)"  
    8.     alt = "Alternate Text"  
    9.     width = "400"  
    10.     height = "100" / >  
    11. }  

You can pass any value for generating barcode in BarcodeImageGenerator methode.