C# 生成条形码
/// <summary> /// 生成条码 /// </summary> private void btnEncode_Click(object sender, EventArgs e) { errorProvider1.Clear(); int W = Convert.ToInt32(this.txtWidth.Text.Trim()); int H = Convert.ToInt32(this.txtHeight.Text.Trim()); int AddNum = Convert.ToInt32(this.tbAddNum.Text.Trim()); int SerialNo = Convert.ToInt32(this.tbSerialNO.Text.Trim()); CodeLib.AlignmentPositions Align = CodeLib.AlignmentPositions.CENTER; switch (cbBarcodeAlign.SelectedItem.ToString().Trim().ToLower()) { case "left": Align = CodeLib.AlignmentPositions.LEFT; break; case "right": Align = CodeLib.AlignmentPositions.RIGHT; break; default: Align = CodeLib.AlignmentPositions.CENTER; break; } CodeLib.TYPE type = CodeLib.TYPE.UNSPECIFIED; switch (cbEncodeType.SelectedItem.ToString().Trim()) { case "Code 128": type = CodeLib.TYPE.CODE128; break; case "Code 128-A": type = CodeLib.TYPE.CODE128A; break; case "Code 128-B": type = CodeLib.TYPE.CODE128B; break; default: MessageBox.Show("请指定编码类型。"); break; } try { if (type != CodeLib.TYPE.UNSPECIFIED) { b.IncludeLabel = this.chkGenerateLabel.Checked; b.Alignment = Align; switch (this.cbLabelLocation.SelectedItem.ToString().Trim().ToUpper()) { case "BOTTOMLEFT": b.LabelPosition = CodeLib.LabelPositions.BOTTOMLEFT; break; case "BOTTOMRIGHT": b.LabelPosition = CodeLib.LabelPositions.BOTTOMRIGHT; break; case "TOPCENTER": b.LabelPosition = CodeLib.LabelPositions.TOPCENTER; break; case "TOPLEFT": b.LabelPosition = CodeLib.LabelPositions.TOPLEFT; break; case "TOPRIGHT": b.LabelPosition = CodeLib.LabelPositions.TOPRIGHT; break; default: b.LabelPosition = CodeLib.LabelPositions.BOTTOMCENTER; break; } int SerialLen = this.tbSerialNO.Text.Trim().Length; string Barcodedata = ""; int i = 0; do { int zeroNum = SerialLen - SerialNo.ToString().Trim().Length; switch (zeroNum) { case 0: Barcodedata = this.txtData.Text.Trim() + SerialNo.ToString(); break; case 1: Barcodedata = this.txtData.Text.Trim() + SerialNo.ToString().Insert(0, "0"); break; case 2: Barcodedata = this.txtData.Text.Trim() + SerialNo.ToString().Insert(0, "00"); break; case 3: Barcodedata = this.txtData.Text.Trim() + SerialNo.ToString().Insert(0, "000"); break; default : Barcodedata = this.txtData.Text.Trim() + SerialNo.ToString().Insert(0, "0000"); break; } barcode.Image = b.Encode(type, Barcodedata.ToString(), W, H); i++; SerialNo++; this.Refresh(); Thread.Sleep(1000); //Printer(); //打印 //Thread.Sleep(500); } while (i< AddNum); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
- 1
- 粉丝: 4
- 资源: 49
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助