Akshay
Akshay

Reputation: 51

Are vector based generators the best way to create barcodes?

Is vector based generators the best way to generate barcodes? If yes, what are the namespaces that it will make use of? How is it used? Can anyone share some knowledge on this?

Upvotes: 2

Views: 1114

Answers (2)

Matt Wilko
Matt Wilko

Reputation: 27342

You don't have to develop barcodes using vector based graphics. I fact have a look at this link on codeproject as most of the work is already done for you. This genrates a bitmap of the required barcode.

Upvotes: 0

Emir Akaydın
Emir Akaydın

Reputation: 5823

Assuming that we are talking about UPC like barcodes, vector based generation is not a must. It's the matter of representing some bits as vertical lines. So, you can easily do this using any graphic library or even using direct access to video buffer. You can represent a single bit with multiple pixels if you need a larger barcode. You don't need to use any interpolation I guess. But if you need a certain size (in pixels/centimeters etc.), vector based solution might be handful but still not a must.

C# source code example for generating scalable barcode graphics.

Steps:

1) Open a new C# Windows Forms sample project named BarCode.

2) Add a PictureBox and change BackColor to White and Dock to Fill.

3) Add Load and Resize events to Form1.

4) Copy & Paste the source code below over Form1.cs file.

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;

namespace BarCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public bool[] barCodeBits;

        private void Form1_Load(object sender, EventArgs e)
        {
            Random r = new Random();
            int numberOfBits = 100;
            barCodeBits = new bool[numberOfBits];
            for(int i = 0; i < numberOfBits; i++) {
                barCodeBits[i] = (r.Next(0, 2) == 1) ? true : false;
            }

            Form1_Resize(null, null);
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            int w = pictureBox1.Width;
            int h = pictureBox1.Height;

            pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            Brush b = new SolidBrush(Color.Black);

            for(int pos = 0; pos < barCodeBits.Length; pos++) {
                if(barCodeBits[pos]) {
                    g.FillRectangle(b, ((float)pos / (float)barCodeBits.Length) * w, 0, (1.0f / (float)barCodeBits.Length) * w, h);
                }
            }
        }
    }
}

Upvotes: 1

Related Questions