基于矢量的生成器是创建条形码的最佳方式吗?

时间:2012-07-19 07:29:17

标签: c# .net c#-4.0

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

2 个答案:

答案 0 :(得分:1)

假设我们在谈论条形码UPC,基于矢量的生成不是必须的。这是将一些位表示为垂直线的问题。因此,您可以使用任何图形库轻松​​完成此操作,甚至可以使用直接访问视频缓冲区。如果需要更大的条形码,则可以使用多个像素表示单个位。我猜你不需要使用任何插值。但是如果你需要一定的尺寸(像素/厘米等),基于矢量的解决方案可能很少但仍然不是必须的。

用于生成可缩放条形码图形的C#源代码示例。

步骤:

1)打开一个名为BarCode的新C#Windows Forms示例项目。

2)添加PictureBox并将BackColor更改为White,将Dock更改为Fill

3)将LoadResize个事件添加到Form1

4)复制&将源代码粘贴到Form1.cs文件上。

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);
                }
            }
        }
    }
}

答案 1 :(得分:0)

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

相关问题