实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BarCodeTest
{
public partial class MainTest : Form
{
public MainTest()
{
InitializeComponent();
}
CodeLib.Barcode b = new CodeLib.Barcode();
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void MainTest_Load(object sender, EventArgs e)
{
Bitmap temp = new Bitmap(1, 1);
temp.SetPixel(0, 0, this.BackColor);
barcode.Image = (Image)temp;
this.cbEncodeType.SelectedIndex = 0;
this.cbBarcodeAlign.SelectedIndex = 0;
this.cbLabelLocation.SelectedIndex = 0;
this.cbRotateFlip.DataSource = System.Enum.GetNames(typeof(RotateFlipType));
int i = 0;
foreach (object o in cbRotateFlip.Items)
{
if (o.ToString().Trim().ToLower() == "rotatenoneflipnone")
break;
i ;
}//foreach
this.cbRotateFlip.SelectedIndex = i;
//Show library version
this.btnBackColor.BackColor = this.b.BackColor;
this.btnForeColor.BackColor = this.b.ForeColor;
}
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());
CodeLib.AlignmentPositions Align = CodeLib.AlignmentPositions.CENTER;
//barcode alignment
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;
}//switch
CodeLib.TYPE type = CodeLib.TYPE.UNSPECIFIED;
switch (cbEncodeType.SelectedItem.ToString().Trim())
{
case "UPC-A": type = CodeLib.TYPE.UPCA; break;
case "UPC-E": type = CodeLib.TYPE.UPCE; break;
case "UPC 2 Digit Ext.": type = CodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break;
case "UPC 5 Digit Ext.": type = CodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break;
case "EAN-13": type = CodeLib.TYPE.EAN13; break;
case "JAN-13": type = CodeLib.TYPE.JAN13; break;
case "EAN-8": type = CodeLib.TYPE.EAN8; break;
case "ITF-14": type = CodeLib.TYPE.ITF14; break;
case "Codabar": type = CodeLib.TYPE.Codabar; break;
case "PostNet": type = CodeLib.TYPE.PostNet; break;
case "Bookland/ISBN": type = CodeLib.TYPE.BOOKLAND; break;
case "Code 11": type = CodeLib.TYPE.CODE11; break;
case "Code 39": type = CodeLib.TYPE.CODE39; break;
case "Code 39 Extended": type = CodeLib.TYPE.CODE39Extended; break;
case "Code 93": type = CodeLib.TYPE.CODE93; break;
case "LOGMARS": type = CodeLib.TYPE.LOGMARS; break;
case "MSI": type = CodeLib.TYPE.MSI_Mod10; break;
case "Interleaved 2 of 5": type = CodeLib.TYPE.Interleaved2of5; break;
case "Standard 2 of 5": type = CodeLib.TYPE.Standard2of5; break;
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;
case "Code 128-C": type = CodeLib.TYPE.CODE128C; break;
case "Telepen": type = CodeLib.TYPE.TELEPEN; break;
default: MessageBox.Show("Please specify the encoding type."); break;
}//switch
try
{
if (type != CodeLib.TYPE.UNSPECIFIED)
{
b.IncludeLabel = this.chkGenerateLabel.Checked;
b.Alignment = Align;
b.RotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), this.cbRotateFlip.SelectedItem.ToString(), true);
//label alignment and position
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;
}//switch
//===== Encoding performed here =====
barcode.Image = b.Encode(type, this.txtData.Text.Trim(), this.btnForeColor.BackColor, this.btnBackColor.BackColor, W, H);
//===================================
//show the encoding time
this.lblEncodingTime.Text = "(" Math.Round(b.EncodingTime, 0, MidpointRounding.AwayFromZero).ToString() "ms)";
txtEncoded.Text = b.EncodedValue;
//tsslEncodedType.Text = "Encoding Type: " b.EncodedType.ToString();
}//if
barcode.Width = barcode.Image.Width;
barcode.Height = barcode.Image.Height;
//reposition the barcode image to the middle
barcode.Location = new Point((this.groupBox2.Location.X this.groupBox2.Width / 2) - barcode.Width / 2, (this.groupBox2.Location.Y this.groupBox2.Height / 2) - barcode.Height / 2);
}//try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}//catch
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif";
sfd.FilterIndex = 2;
sfd.AddExtension = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
CodeLib.SaveTypes savetype = CodeLib.SaveTypes.UNSPECIFIED;
switch (sfd.FilterIndex)
{
case 1: /* BMP */ savetype = CodeLib.SaveTypes.BMP; break;
case 2: /* GIF */ savetype = CodeLib.SaveTypes.GIF; break;
case 3: /* JPG */ savetype = CodeLib.SaveTypes.JPG; break;
case 4: /* PNG */ savetype = CodeLib.SaveTypes.PNG; break;
case 5: /* TIFF */ savetype = CodeLib.SaveTypes.TIFF; break;
default: break;
}//switch
b.SaveImage(sfd.FileName, savetype);
}
}
private void btnForeColor_Click(object sender, EventArgs e)
{
using (ColorDialog cdialog = new ColorDialog())
{
cdialog.AnyColor = true;
if (cdialog.ShowDialog() == DialogResult.OK)
{
this.b.ForeColor = cdialog.Color;
this.btnForeColor.BackColor = this.b.ForeColor;
}//if
}//using
}
private void btnBackColor_Click(object sender, EventArgs e)
{
using (ColorDialog cdialog = new ColorDialog())
{
cdialog.AnyColor = true;
if (cdialog.ShowDialog() == DialogResult.OK)
{
this.b.BackColor = cdialog.Color;
this.btnBackColor.BackColor = this.b.BackColor;
}//if
}//using
}
private void btnSaveXML_Click(object sender, EventArgs e)
{
btnEncode_Click(sender, e);
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "XML Files|*.xml";
if (sfd.ShowDialog() == DialogResult.OK)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName))
{
sw.Write(b.XML);
}//using
}//if
}//using
}
private void btnLoadXML_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
using (CodeLib.BarcodeXML XML = new CodeLib.BarcodeXML())
{
XML.ReadXml(ofd.FileName);
//load image from xml
this.barcode.Width = XML.Barcode[0].ImageWidth;
this.barcode.Height = XML.Barcode[0].ImageHeight;
this.barcode.Image = CodeLib.Barcode.GetImageFromXML(XML);
//populate the screen
this.txtData.Text = XML.Barcode[0].RawData;
this.chkGenerateLabel.Checked = XML.Barcode[0].IncludeLabel;
switch (XML.Barcode[0].Type)
{
case "UCC12":
case "UPCA":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("UPC-A");
break;
case "UCC13":
case "EAN13":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("EAN-13");
break;
case "Interleaved2of5":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Interleaved 2 of 5");
break;
case "Industrial2of5":
case "Standard2of5":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Standard 2 of 5");
break;
case "LOGMARS":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("LOGMARS");
break;
case "CODE39":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Code 39");
break;
case "CODE39Extended":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Code 39 Extended");
break;
case "Codabar":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Codabar");
break;
case "PostNet":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("PostNet");
break;
case "ISBN":
case "BOOKLAND":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Bookland/ISBN");
break;
case "JAN13":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("JAN-13");
break;
case "UPC_SUPPLEMENTAL_2DIGIT":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("UPC 2 Digit Ext.");
break;
case "MSI_Mod10":
case "MSI_2Mod10":
case "MSI_Mod11":
case "MSI_Mod11_Mod10":
case "Modified_Plessey":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("MSI");
break;
case "UPC_SUPPLEMENTAL_5DIGIT":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("UPC 5 Digit Ext.");
break;
case "UPCE":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("UPC-E");
break;
case "EAN8":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("EAN-8");
break;
case "USD8":
case "CODE11":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Code 11");
break;
case "CODE128":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Code 128");
break;
case "CODE128A":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Code 128-A");
break;
case "CODE128B":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Code 128-B");
break;
case "CODE128C":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Code 128-C");
break;
case "ITF14":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("ITF-14");
break;
case "CODE93":
this.cbEncodeType.SelectedIndex = this.cbEncodeType.FindString("Code 93");
break;
default: throw new Exception("ELOADXML-1: Unsupported encoding type in XML.");
}//switch
this.txtEncoded.Text = XML.Barcode[0].EncodedValue;
this.btnForeColor.BackColor = ColorTranslator.FromHtml(XML.Barcode[0].Forecolor);
this.btnBackColor.BackColor = ColorTranslator.FromHtml(XML.Barcode[0].Backcolor); ;
this.txtWidth.Text = XML.Barcode[0].ImageWidth.ToString();
this.txtHeight.Text = XML.Barcode[0].ImageHeight.ToString();
//populate the local object
btnEncode_Click(sender, e);
//reposition the barcode image to the middle
barcode.Location = new Point((this.groupBox2.Location.X this.groupBox2.Width / 2) - barcode.Width / 2, (this.groupBox2.Location.Y this.groupBox2.Height / 2) - barcode.Height / 2);
}//using
}//if
}//using
}
}
}
标签: 二维码
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论