利用ZXing生成条码二维码例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using ZXing.Common; using ZXing; using ZXing.QrCode; using System.Windows.Forms; using System.Text.RegularExpressions; namespace BarCodePrint { class BarCodeGenerate { ///<summary> ///生成条形码 ///</summary> ///<paramname="pictureBox1"></param> ///<paramname="Contents"></param> public Bitmap CreateBarCodeBitMap(string Contents, int Width, int Height) { //Regex rg = new Regex("^[0-9]{12}$"); //if (!rg.IsMatch(Contents)) //{ // MessageBox.Show("本例子采用EAN_13编码,需要输入12位数字"); // return; //} EncodingOptions options = null; BarcodeWriter writer = null; options = new EncodingOptions { Width = Width, Height = Height }; writer = new BarcodeWriter(); writer.Format = BarcodeFormat.CODE_128; writer.Options = options; Bitmap bitmap = writer.Write(Contents); return bitmap; } ///<summary> ///生成条形码 ///</summary> ///<paramname="pictureBox1"></param> ///<paramname="Contents"></param> public void CreateBarCode(PictureBox pictureBox1, string Contents) { //Regex rg = new Regex("^[0-9]{12}$"); //if (!rg.IsMatch(Contents)) //{ // MessageBox.Show("本例子采用EAN_13编码,需要输入12位数字"); // return; //} EncodingOptions options = null; BarcodeWriter writer = null; options = new EncodingOptions { Width = pictureBox1.Width, Height = pictureBox1.Height }; writer = new BarcodeWriter(); writer.Format = BarcodeFormat.CODE_128; writer.Options = options; Bitmap bitmap = writer.Write(Contents); pictureBox1.Image = bitmap; } ///<summary> ///生成二维码 ///</summary> ///<paramname="pictureBox1"></param> ///<paramname="Contents"></param> public Bitmap CreateQRCode(string Contents, int Width, int Height) { if (Contents == string.Empty) { MessageBox.Show("输入内容不能为空!"); } EncodingOptions options = null; BarcodeWriter writer = null; options = new QrCodeEncodingOptions { DisableECI = true, CharacterSet = "UTF-8", Width = Width, Height = Height }; writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; Bitmap bitmap = writer.Write(Contents); return bitmap; } ///<summary> ///生成二维码 ///</summary> ///<paramname="pictureBox1"></param> ///<paramname="Contents"></param> public void CreateQuickMark(PictureBox pictureBox1, string Contents) { if (Contents == string.Empty) { MessageBox.Show("输入内容不能为空!"); return; } EncodingOptions options = null; BarcodeWriter writer = null; options = new QrCodeEncodingOptions { DisableECI = true, CharacterSet = "UTF-8", Width = pictureBox1.Width, Height = pictureBox1.Height }; writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; Bitmap bitmap = writer.Write(Contents); pictureBox1.Image = bitmap; } ///<summary> ///解码 ///</summary> ///<paramname="pictureBox1"></param> public void Decode(PictureBox pictureBox1) { BarcodeReader reader = new BarcodeReader(); Result result = reader.Decode((Bitmap)pictureBox1.Image); } } }
本文来自博客园,作者:CelonY,转载请注明原文链接:https://www.cnblogs.com/CelonY/p/14721351.html