Generate Barcode in Windows Form Using C#

In the code chamber, you need to write the code, like whatever the TextBox contains is printed in the form of the bardcode on a button click. To make this application you first need to install the IDAutomationCode39.exe file. Open your code section and start writing the following code so that you can get to the solution.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
namespace Barcode_demo {
    public partial class Form1: Form {
        public Form1() {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e) {
            string barcode = textBox1.Text;
            Bitmap bitm = new Bitmap(barcode.Length * 45, 160);
            using(Graphics graphic = Graphics.FromImage(bitm)) {
                Font newfont = new Font(“IDAutomationHC39M”, 20);
                PointF point = new PointF(2f, 2f);
                SolidBrush black = new SolidBrush(Color.Black);
                SolidBrush white = new SolidBrush(Color.White);
                graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
                graphic.DrawString(“*” + barcode + “*”, newfont, black, point);
            }
            using(MemoryStream Mmst = new MemoryStream()) {
                bitm.Save(“ms”, ImageFormat.Jpeg);
                pictureBox1.Image = bitm;
                pictureBox1.Width = bitm.Width;
                pictureBox1.Height = bitm.Height;
            }
        }
    }
}

Once you install the IDAutomationCode39 file, just ensure you restart your system, otherwise Visual Studio will throw an error message. You may get stuck with the following error message:
“Only TrueType fonts are supported and this is not a TrueType font”
Resolve the error by the following two methods:

  • After Installing restart your system or Visual Studio
  • Or you need to embed this code.

private void Form1_Load(object sender, System.EventArgs e)
{
    label1.Font = new Font(“IDAutomationHC39M”, 12, FontStyle.Regular);
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*