c#生成条形码

一、生成EAN13的一维码
// 1.设置条形码规格
EncodingOptions encodeOption = new EncodingOptions();
encodeOption.Height = 130; // 必须制定高度、宽度
encodeOption.Width = 240;
 
// 2.生成条形码图片并保存
ZXing.BarcodeWriter wr = new BarcodeWriter();
wr.Options = encodeOption;
wr.Format = BarcodeFormat.EAN_13; //  条形码规格:EAN13规格:12(无校验位)或13位数字
Bitmap img = wr.Write(this.ContentTxt.Text); // 生成图片
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\EAN_13-" + this.ContentTxt.Text + ".jpg";
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
 二、读取一维码
// 1.设置读取条形码规格
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>() {
   BarcodeFormat.EAN_13,
};
 
// 2.进行读取操作
ZXing.BarcodeReader br = new BarcodeReader();
br.Options = decodeOption;
ZXing.Result rs = br.Decode(this.barCodeImg.Image as Bitmap);
if (rs == null)
{
    this.ContentTxt.Text = "读取失败";
    MessageBox.Show("读取失败");
}
else
{
    this.ContentTxt.Text = rs.Text;
    MessageBox.Show("读取成功,内容:" + rs.Text);
}
posted @ 2017-10-03 13:23  插里一试  阅读(6178)  评论(0编辑  收藏  举报