vb.net

Generate Barcode In ASP.NET using VB.NET

In this article we will learn with an example how to Generate Barcode In ASP.NET using VB.NET.

In this blog, we will learn how to Generate Barcode In ASP.NET by entering numbers and alphabet letters in the textbox and clicking Generate button.

Generate Barcode In ASP.NET using VB.NET

Step 1. First we will need to download the Free Barcode Font from the given below link.
Please Download Barcode

Step 2. First Extract the zip file and install on your system. Open the file and then click on Install button like screenshot. After installation, restart your system.

generate-barcode-in-asp-net-using-vb-net

Step 3. Create a project in the Visual Studio. Give it a meaningful name.

Step 4.In this step we will add web form right on the project from solution explorer, add new item, choose web form and give it a name as per your choice.

Step 5. Design HTML web form


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
   .txtBarcode {
  width: 20%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}
.btnGenerateBarCode:hover {
  background-color: #45a049;
}
.btnGenerateBarCode {
  width: 10%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.div1 {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}
    </style>
</head>
    
<body>
    <form id="form1" runat="server">
   <div id="div1" class="div1">
           <h2 class="text-center text-uppercase">How to create barcode in asp.net</h2>  
             <label for="fname">Enter Value :</label> 
            <asp:TextBox ID="txtBarcode" CssClass="txtBarcode"  runat="server"></asp:TextBox>
            <asp:Button ID="btnGenerateBarCode" runat="server" CssClass="btnGenerateBarCode" Text="Generate" OnClick="btnGenerateBarCode_Click" />
            <hr />
           
            <asp:Image ID="imgBarcode" runat="server" Visible="false" />
             <hr />
        </div>
           
    </form>
</body>
</html>

Step 6. Please Add Namespaces.

Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging

Generate Barcode In ASP.NET using VB.NET

VB.NET Code


       Protected Sub btnGenerateBarCode_Click(ByVal sender As Object, ByVal e As EventArgs)
            GenerateBarCode()
        End Sub

        Public Sub GenerateBarCode()
            Dim barCode As String = txtBarcode.Text

            Using bitMap As Bitmap = New Bitmap(barCode.Length * 40, 80)

                Using graphics As Graphics = Graphics.FromImage(bitMap)
                    Dim oFont As Font = New Font("IDAutomationHC39M Free Version", 16)
                    Dim point As PointF = New PointF(2F, 2F)
                    Dim blackBrush As SolidBrush = New SolidBrush(Color.Black)
                    Dim whiteBrush As SolidBrush = New SolidBrush(Color.White)
                    graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
                    graphics.DrawString("*" & barCode & "*", oFont, blackBrush, point)
                End Using

                Using ms As MemoryStream = New MemoryStream()
                    bitMap.Save(ms, ImageFormat.Png)
                    Dim byteImage As Byte() = ms.ToArray()
                    imgBarcode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(byteImage)
                    imgBarcode.Visible = True
                End Using
            End Using
        End Sub

Output

generate-barcode-in-asp-net-using-vb-net

Leave a Reply

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