C#.NET - Generate Barcode

In my previous articles, we explored diverse facets of C#.NET, from creating a Speech Translator to exporting DataGridView to Excel and crafting User Controls. Today, let's delve into the fascinating realm of barcode generation in C#.NET.

Previous Article Recap

Before we dive into barcode generation, let's recap our previous articles:

  1. Speech Translator in C#.NET: Learn to transform text into speech using C#.NET. Read More
  2. Datagridview Export to Excel in C#.NET: Explore the seamless process of exporting data from a DataGridView to an Excel sheet.Read More
  3. Create User Control in C#.NET: Discover the art of crafting user controls for enhanced reusability in your C#.NET projects. Read More

Generate Barcode with C#.NET

Understanding Barcodes

A barcode is a machine-readable code comprising numbers and a pattern of parallel lines of varying widths. It's widely used for stock control and efficient data management. Today, we'll explore how to generate barcodes using a Windows Application in C#.NET.

how can we generate and print a barcode stricker using C#

Prerequisites

Before we embark on the barcode generation journey, ensure you've downloaded and installed the necessary barcode font. Download Barcode Font for Free

Integrating Barcode Font with Visual Studio

  • Right-click on a button, choose properties, and select Font.
  • Open the Font window and enter the letter 'i' in the font textbox.
  • Ensure the font resembles the provided image.
Font for Barcode | Barcode Font

C# Coding: Making it Happen

Let's dive into the coding aspect of barcode generation in C#.NET.

Namespaces


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;

Get Barcode Button Click Event


namespace WindowsFormsApplication1
{
    public partial class generatebarcode : Form
    {
        public generatebarcode()
        {
            InitializeComponent();
        }

        private void Btn_Generate_Click(object sender, EventArgs e)
        {
            string barcode = textBox1.Text;
            Bitmap bitmap = new Bitmap(barcode.Length * 40, 150);
            using (Graphics ographics = Graphics.FromImage(bitmap))
            {
                Font oFont = new System.Drawing.Font("IDAutomationHC39M", 20);
                PointF point = new PointF(2f, 2f);
                SolidBrush oblack = new SolidBrush(Color.Black);
                SolidBrush owhite = new SolidBrush(Color.White);
                ographics.FillRectangle(owhite, 0, 0, bitmap.Width, bitmap.Height);
                ographics.DrawString("*" + barcode + "*", oFont, oblack, point);
            }
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, ImageFormat.Png);
                pb_barcode.Image = bitmap;
                pb_barcode.Height = bitmap.Height;
                pb_barcode.Width = bitmap.Width;
            }
        }
    }
}

Download the Project Source Code

To access the design and source code for barcode generation in C#.NET, click here.

Conclusion

Barcodes play a pivotal role in modern data management, offering efficiency and accuracy. With this comprehensive guide, you are now equipped to seamlessly integrate barcode generation into your C#.NET projects. Download the source code, experiment, and elevate your coding prowess. Happy coding!

Post a Comment

0 Comments