您的位置:首页 > 编程语言 > C#

基于C#的BarCode 39实现

2015-05-13 22:40 316 查看
一、39条码简介

39码是1974年发展出来的条码系统,是一种可供使用者双向扫瞄的分散式条码,也就是说相临两资料码之间,必须包含一个不具任何意义的空白(或细白,其逻辑值为0),且其具有支援文字的能力,故较一般一维条码应用广泛,由于CODE 39支持0~9、A~Z等,目前较主要用于,通常运用于资产管理、会员卡、店内码管理、产品卷标等。

标准的39码是由起始安全空间、起始码、资料码、可忽略不计的检查码、终止安全空间及终止码所构成。

二、39码编码

  39码的每一个字元编码方式,都是由九条不同排列的线条编码而得。可区分成如表之四种类型:

private void btnEncode_Click(object sender, EventArgs e)
{
string input = this.txtInput.Text.Trim();
string txtwidth = this.txtWidth.Text.Trim();
string txtheight = this.txtHeight.Text.Trim();
if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(txtwidth) && !string.IsNullOrEmpty(txtheight))
{
this._width = Int32.Parse(txtwidth);
this._height = Int32.Parse(txtheight);
barCode = new BarCode(input, true, this._height, this._width, true);
string codeValue = string.Empty;
pb.Image = barCode.GenerateCodeImage(ref codeValue);
//reposition the barcode image to the middle
pb.Location = new Point((this.pb.Location.X + this.pb.Width / 2) - this.pb.Width / 2, (this.pb.Location.Y + this.pb.Height / 2) - pb.Height / 2);

//txtEncoded.Text = codeValue;
}
else
{
MessageBox.Show("请输入完整信息");
}
}
/// <summary>
/// 打印生成的条形码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
if (printDialog.ShowDialog(this) == DialogResult.OK) //到这里会出现选择打印项的窗口
{
printDocument.Print(); //到这里会出现给文件命名的窗口,点击确定后进行打印并完成打印
}
}
/// <summary>
/// 使用PrintDocument,并且设置相应的事件监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
if (pb.Image != null)
{
Rectangle rect = new Rectangle(50, 50, this._width, this._height);//设置打印起始位置和大小
e.Graphics.DrawImage(pb.Image,rect);//确定要打印的图片及其打印起始位置和大小
e.HasMorePages = false;
}
}
catch (Exception ex)
{
MessageBox.Show("请先生成条形码!");
}
}

private void btnSaveAsImg_Click(object sender, EventArgs e)
{
SaveFileDialog saveFdlg = new SaveFileDialog();
saveFdlg.Filter = "*.png|*.jpg";
if (saveFdlg.ShowDialog() == DialogResult.OK)
{
string fileName = saveFdlg.FileName;
barCode.SaveImage(fileName);
}
}


View Code
(3)效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: