Thursday, 18 October 2012

Generate Barcode in windows application

Let's take a look step by step.

Step 1: Download QRCODE GENERATOR LIBRARY from onbarcode.com.

Step 2: Open Visual Studio - Create New Project - Windows Form.

Step 3: Add reference to OnBarcode.Barcode.Winforms.dll.

Step 4: Design form with some input fields for accepting data to encode and the targeted location to save barcode generated image.

Step 5: To generate Barcode as well as Qrcode images write two differen methods as follows.
 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 OnBarcode.Barcode;
using System.Drawing.Imaging;
namespace DRAWBARCODEAPP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
        private void GenerateQrcode(string _data, string _filename)
        {
            QRCode qrcode = new QRCode();
            qrcode.Data = _data;
            qrcode.DataMode = QRCodeDataMode.Byte;
            qrcode.UOM = UnitOfMeasure.PIXEL;
            qrcode.X = 3;
            qrcode.LeftMargin = 0;
            qrcode.RightMargin = 0;
            qrcode.TopMargin = 0;
            qrcode.BottomMargin = 0;
            qrcode.Resolution = 72;
            qrcode.Rotate = Rotate.Rotate0;
            qrcode.ImageFormat = ImageFormat.Gif;
            qrcode.drawBarcode(_filename);
        }
        private void GenerateBacode(string _data, string _filename)
        {
            Linear barcode = new Linear();
            barcode.Type = BarcodeType.CODE11;
            barcode.Data = _data;
            barcode.drawBarcode(_filename);
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.ShowDialog();
            textBox2.Text = sfd.FileName.ToString()+".gif";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            GenerateBacode(textBox1.Text, textBox2.Text);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            GenerateQrcode(textBox1.Text, textBox2.Text);
        }
    }
}

Now Take look on the Windows form:-

After running this form it will display as

By click on Barcode Button it will create following Jpg image
By click on Qrcode it will create following jpg image

Download this application with library
Download Files:

No comments:

Post a Comment