In this article I will explain with an example, how to dynamically generate and display barcode image in Windows Application using C# and VB.Net.
Note: The IDAutomationHC39M Free Version Barcode Font is provided by IDAutomation and they are the creators and owners of the Font and its license.
 
 
Downloading and installing Barcode Font
First you will need to download the Free Barcode Font from the following link.
Once downloaded follow the following steps.
1. Extract the Font from the ZIP file.
2. Double Click, Open the File and then click the Install Button as shown below.
Generate Barcode in Windows Forms (WinForms) Application using C# and VB.Net
 
3. After installation is completed, restart your machine.
 
 
Form Design
You will need to add a TextBox, a Button and a PictureBox control to the Windows Form.
Generate Barcode in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Drawing.Imaging;
 
VB.Net
Imports System.IO
Imports System.Drawing.Imaging
 
 
Barcode Generation
The following event handler is executed when the Button is clicked. Using the text from the TextBox, first the length of the Barcode image is determined.
Then using the Graphics class a white rectangle is drawn and then on the rectangle the barcode is written using Barcode font.
Finally the Bitmap image is displayed in the PictureBox control.
C#
private void btnGenerate_Click(object sender, EventArgs e)
{
    string barCode = txtCode.Text;
    Bitmap bitMap = new Bitmap(barCode.Length * 40, 80);
 
    using (Graphics graphics = Graphics.FromImage(bitMap))
    {
        Font oFont = new Font("IDAutomationHC39M Free Version", 16);
        PointF point = new PointF(2f, 2f);
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        SolidBrush whiteBrush = new SolidBrush(Color.White);
        graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
        graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
    }
    using (MemoryStream ms = new MemoryStream())
    {
        bitMap.Save(ms, ImageFormat.Png);
        pictureBox1.Image = bitMap;
        pictureBox1.Height = bitMap.Height;
        pictureBox1.Width = bitMap.Width;
    }
}
 
VB.Net
Private Sub btnGenerate_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerate.Click
    Dim barCode As String = txtCode.Text
    Dim bitMap As New Bitmap(barCode.Length * 40, 80)
 
    Using graphics__1 As Graphics = Graphics.FromImage(bitMap)
        Dim oFont As New Font("IDAutomationHC39M Free Version", 16)
        Dim point As New PointF(2.0F, 2.0F)
        Dim blackBrush As New SolidBrush(Color.Black)
        Dim whiteBrush As New SolidBrush(Color.White)
        graphics__1.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
        graphics__1.DrawString((Convert.ToString("*") & barCode) + "*", oFont, blackBrush, point)
    End Using
    Using ms As New MemoryStream()
        bitMap.Save(ms, ImageFormat.Png)
        pictureBox1.Image = bitMap
        pictureBox1.Height = bitMap.Height
        pictureBox1.Width = bitMap.Width
    End Using
End Sub
 
 
Screenshot
Generate Barcode in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Downloads