.net 实现条码

我们先来看一下效果图:

现在一般的商品条码都是采用Code 39 码,就是十三位,

本例子中对于任意的一种码次都可以生成,由于时间的仓促,

本例子中默认的设置为Code 128 ,后期都把相应的功能改善的更加的完善,

因为先前自己做一块的时候,找了许多的资料,很是痛苦,现在拿出来与大家一起分享,

本例子中的PrintDocument 类进行了封装,需要的朋友请留下自己的Email,会一一给大家

发送,现在进入正题:

代码:

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.Text.RegularExpressions;
using PrintDocument;

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

#region 设计开发说明
#region 用途
#endregion
#region 作用方式
#endregion
#region 设计备注
#endregion
#region 开发备注
#endregion
#region 变动记录

//Ver 1.0.0.1 (2010-12-06) [Andy20101987] [实现条码的自动生成]
#endregion
#endregion

#region 代码
#region 构造函数
#endregion
#region 附加属性
#endregion
#region 属性
#region 变量
#endregion
#region 常量

/// <summary>
/// 实例化条码工具类
/// </summary>
private BarcodeLib.Barcode barcode = new BarcodeLib.Barcode();

/// <summary>
/// 设定条码编码类型为 CODE128 码
/// </summary>
private BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;

#endregion

#endregion
#region 成员方法
#endregion
#region 事件

/// <summary>
/// 生成条码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tspTxtOk_Click(object sender, EventArgs e)
{
if (!this.tspTxtBarcode.Text.Equals(""))
{
if (Regex.IsMatch(this.tspTxtBarcode.Text, "^[a-zA-Z0-9]*$"))
{
type
= BarcodeLib.TYPE.CODE128;

if (type != BarcodeLib.TYPE.UNSPECIFIED)
{

//是否显示输入的条码信息
barcode.IncludeLabel = true;

//绘画出条码
this.pictureBox1.Image = barcode.Encode(type, this.tspTxtBarcode.Text, Color.Black, Color.White, 200, 100);

}
}
else
{

MessageBox.Show(
"非法字符!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
}
else
{

MessageBox.Show(
"不能为空!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tspTxtExit_Click(object sender, EventArgs e)
{
Close();
}

#endregion
#endregion
}

}

实现起来很简单,希望给大家提供了方便

如需转载,但请注明文章来源和超链接等版权信息,谢谢合作!
原文地址:https://www.cnblogs.com/zhenlin/p/1897667.html