2

基于矢量的生成器是生成条形码的最佳方式吗?如果是,它将使用哪些命名空间?它是如何使用的?任何人都可以分享一些这方面的知识吗?

4

2 回答 2

1

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);
                }
            }
        }
    }
}
于 2012-07-19T07:34:55.590 回答
0

您不必使用基于矢量的图形来开发条形码。事实上,我已经在 codeproject 上查看了这个链接,因为大部分工作已经为你完成了。这会生成所需条形码的位图。

于 2012-07-19T13:48:34.180 回答