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);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM