MVC Web Application BarCode Image Genarater

This is a simple library that lets you do one thing very easily: generate an Image for a Code128 BARCODE with a single line of code.

Code128, on the other hand, has out-of-the-box support for all 128 low-order ASCII characters.  

Basic usage 
 
Create MVC web application. Below code add to the View which it will take input string and one button to generate BarCode Image.
 
 
 
 Now Add the below code in controller (HTTPPOST). Here get the entered text and send that text to
 
 Code128Rendering.MakeBarcodeImage(Bacodetext.Trim(), int.Parse(txtWeight), true) 
 
Method which it will take the input as

There are three parameters:

  • string InputDataThe message to be encoded
  • int BarWeight: The baseline width of the bars in the output. Usually, 1 or 2 is good.
  • bool AddQuietZoneIf false, omits the required white space at the start and end of the barcode. If your layout doesn't already provide good margins around the Image, you should use true
You can save the images in folder location different type (.bmp/ .png / .jpeg)
  1. [HttpPost]  
  2.       public ActionResult Index(string btnSubmit, FormCollection formcoll)  
  3.       {  
  4.           try  
  5.           {  
  6.               if (!ReferenceEquals(formcoll["btnGenarate"], null))  
  7.               {  
  8.                   string Bacodetext = formcoll["txtInput"].ToString();  
  9.                   string txtWeight = "2";  
  10.                   const int MaxLength = 10;  
  11.                   // Genarate barcode  
  12.                   Image myimg = Code128Rendering.MakeBarcodeImage(Bacodetext.Trim(), int.Parse(txtWeight), true);  
  13.                   var imgName = Bacodetext;  
  14.                   if (imgName.Length > MaxLength)  
  15.                   {  
  16.                       imgName = imgName.Substring(0, MaxLength);  // Image name trim  
  17.                   }  
  18.                   // save image in folder location different type (.bmp/ .png / .jpeg)  
  19.                   string ImageFolder = Server.MapPath("~/Images");  
  20.                   myimg.Save(ImageFolder + "/" + imgName.Trim() + ".bmp");   
  21.                   
  22.                   //image to displa in view  
  23.                   var virtualPath = string.Format("~/Images/{0}.bmp", imgName.Trim());  
  24.                   ViewBag.Path = virtualPath;  
  25.               }    
  26.           }  
  27.           catch (Exception ex)  
  28.           {  
  29.           }  
  30.           return View();  
  31.       }  
  32.   
  33.   }  
Note: Add library files Code128Content.cs & Code128Rendering.cs in Utility folder. (That's the GenCode128 namespace, in a static class called Code128Rendering)
 
 
 
Output
 
 
 
 Hopefully, this helps you to generate barcode using web application.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now