How to generate Barcode in Windows Forms Application using C#

193 Views

Namespaces
using System.IO;
using System.Drawing.Imaging;
Code
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”, 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;
}
}

Skip to toolbar