Saturday, October 30, 2021

How To Generate Barcode In ASP.NET

 In this blog, we will learn how to generate a barcode using asp.net by simply entering numbers and alphabet letters in the textbox and clicking generate. (I have added a downloaded file in zip folder of the project.)

Step 1

Download the barcode font from the link given below:

https://www.idautomation.com/free-barcode-products/code39-font/

Step 2

Extract the zip file and install on your system. After installation, restart your system.

Step 3

Create an empty project in the Visual Studio version of your choice. Give it a meaningful name.

Step 4

Add web form right on the project from solution explorer, add new item, choose web form and give it a name of your choice.

Step 5

Add script and styles cdn link in head section of your web form.

  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">  
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>  

Step 6 - Design HTML web form

Drag and drop TextBox , Button and PlaceHolder control:
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div class="container">  
  4.             <h3 class="text-center text-uppercase">How to create barcode in asp.net</h3>  
  5.             <div class="row">  
  6.                 <div class="form-group">  
  7.                     <label>Enter Number:</label>  
  8.                     <div class="input-group">  
  9.                         <asp:TextBox ID="txtBarcode" CssClass="form-control" runat="server"></asp:TextBox>  
  10.                         <div class="input-group-append">  
  11.                             <asp:Button ID="btnGenrate" CssClass="btn btn-outline-primary" runat="server" Text="Genrate" OnClick="btnGenrate_Click" />                             
  12.                         </div>  
  13.                     </div>  
  14.                 </div>                  
  15.             </div>  
  16.             <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>  
  17.         </div>  
  18.     </form>  
  19. </body>  

Step 7

Double click or enter Button control.

Add namespace

using System;
using System.Drawing;
using System.IO;

Write the following code

  1. protected void btnGenrate_Click(object sender, EventArgs e)  
  2.         {  
  3.             string barCode = txtBarcode.Text;  
  4.             System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();  
  5.             using (Bitmap bitMap = new Bitmap(barCode.Length * 20, 80))  
  6.             {  
  7.                 using (Graphics graphics = Graphics.FromImage(bitMap))  
  8.                 {  
  9.                     Font oFont = new Font("IDAutomationHC39M", 16);  
  10.                     PointF point = new PointF(2f, 2f);  
  11.                     SolidBrush blackBrush = new SolidBrush(Color.Black);  
  12.                     SolidBrush whiteBrush = new SolidBrush(Color.White);  
  13.                     graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);  
  14.                     graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);  
  15.                 }  
  16.                 using (MemoryStream ms = new MemoryStream())  
  17.                 {  
  18.                     bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);  
  19.                     byte[] byteImage = ms.ToArray();  
  20.   
  21.                     Convert.ToBase64String(byteImage);  
  22.                     imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);  
  23.                 }  
  24.                 PlaceHolder1.Controls.Add(imgBarCode);  
  25.             }  
  26.         }  

Step 8

Run project ctr+F5

Final output 

ASP.NET

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...