private void generate_barcode(String queryString, int width, int height, PictureBox targetPictureBox) { Regex rg = new Regex("^[0-9]{11}$"); if (!rg.IsMatch(queryString)) { // MessageBox.Show("本例子采用EAN_13编码,需要输入13位数字"); MessageBox.Show("本例子采用EAN_11编码,需要输入11位数字"); return; } try { MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); ByteMatrix bm = mutiWriter.encode(queryString, com.google.zxing.BarcodeFormat.EAN_13, 363, 150); Bitmap img = bm.ToBitmap(); targetPictureBox.Image = img; //自动保存图片到当前目录 //string filename = System.Environment.CurrentDirectory + "\\EAN_13" + DateTime.Now.Ticks.ToString() + ".jpg"; //img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ee) { MessageBox.Show(ee.Message); } }
//生成条形码 private void button1_Click(object sender, EventArgs e) { lbshow.Text = ""; Regex rg = new Regex("^[0-9]{13}$"); if (!rg.IsMatch(txtMsg.Text)) { MessageBox.Show("本例子采用EAN_13编码,需要输入13位数字"); return; } try { MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.EAN_13, 363, 150); Bitmap img = bm.ToBitmap(); pictureBox1.Image = img; //自动保存图片到当前目录 string filename = System.Environment.CurrentDirectory + "\\EAN_13" + DateTime.Now.Ticks.ToString() + ".jpg"; img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); lbshow.Text = "图片已保存到:" + filename; } catch (Exception ee) { MessageBox.Show(ee.Message); } }
/// <summary> /// 生成二维码 /// </summary> private void button2_Click(object sender, EventArgs e) { lbshow.Text = ""; if (string.IsNullOrEmpty(txtMsg.Text)) { lbshow.Text = "请输入内容!"; return; } try { //构造二维码写码器 com.google.zxing.MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); Bitmap img = bm.ToBitmap(); pictureBox1.Image = img; //自动保存图片到当前目录 string filename = GetSaveImagePath("QR"); img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); lbshow.Text = "图片已保存到:" + filename; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 生成条形码 /// </summary> private void button1_Click(object sender, EventArgs e) { lbshow.Text = ""; Regex rg = new Regex("^[0-9]{13}$"); if (!rg.IsMatch(txtMsg.Text)) { lbshow.Text = "请输入13位数字!"; return; } try { //本例子采用EAN_13编码 com.google.zxing.MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.EAN_13, 363, 150); Bitmap img = bm.ToBitmap(); pictureBox1.Image = img; //自动保存图片到当前目录 string filename = GetSaveImagePath("EAN13"); img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); lbshow.Text = "图片已保存到:" + filename; } catch (Exception ee) { MessageBox.Show(ee.Message); } }
/// <summary> /// 生成中间带图片的二维码 /// </summary> private void button5_Click(object sender, EventArgs e) { lbshow.Text = ""; if (string.IsNullOrEmpty(txtMsg.Text)) { lbshow.Text = "请输入内容!"; return; } try { //构造二维码写码器 com.google.zxing.MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); Hashtable hint = new Hashtable(); hint.Add(com.google.zxing.EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(com.google.zxing.EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H); //生成二维码 ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300, hint); Bitmap img = bm.ToBitmap(); //要插入到二维码中的图片 Image middlImg = QRMiddleImg.Image; //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) System.Drawing.Size realSize = mutiWriter.GetEncodeSize(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); //计算插入图片的大小和位置 int middleImgW = Math.Min((int)(realSize.Width / 3.5), middlImg.Width); int middleImgH = Math.Min((int)(realSize.Height / 3.5), middlImg.Height); int middleImgL = (img.Width - middleImgW) / 2; int middleImgT = (img.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(img, 0, 0); } //在二维码中插入图片 System.Drawing.Graphics MyGraphic = System.Drawing.Graphics.FromImage(bmpimg); //白底 MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH); pictureBox1.Image = bmpimg; //自动保存图片到当前目录 string filename = GetSaveImagePath("QR"); bmpimg.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); lbshow.Text = "图片已保存到:" + filename; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void generate_qrcode(String queryString, Image logoImage, int size, PictureBox targetPictureBox) { try { //构造二维码写码器 MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); Hashtable hint = new Hashtable(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H); //生成二维码 ByteMatrix bm = mutiWriter.encode(queryString, com.google.zxing.BarcodeFormat.QR_CODE, size, size, hint); Bitmap img = bm.ToBitmap(); //要插入到二维码中的图片 Image middlImg = logoImage; //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) System.Drawing.Size realSize = mutiWriter.GetEncodeSize(TrackingNumber.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); //计算插入图片的大小和位置 int middleImgW = Math.Min((int)(realSize.Width / 3.5), middlImg.Width); int middleImgH = Math.Min((int)(realSize.Height / 3.5), middlImg.Height); int middleImgL = (img.Width - middleImgW) / 2; int middleImgT = (img.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(img, 0, 0); } //在二维码中插入图片 System.Drawing.Graphics MyGraphic = System.Drawing.Graphics.FromImage(bmpimg); //白底 MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH); targetPictureBox.Image = bmpimg; //自动保存图片到当前目录 //string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg"; //bmpimg.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); //lbshow.Text = "图片已保存到:" + filename; } catch (Exception ee) { MessageBox.Show(ee.Message); } }
//生成二维码 private void button2_Click(object sender, EventArgs e) { lbshow.Text = ""; try { MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); Bitmap img = bm.ToBitmap(); pictureBox1.Image = img; //自动保存图片到当前目录 string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg"; img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); lbshow.Text = "图片已保存到:" + filename; } catch (Exception ee) { MessageBox.Show(ee.Message); } }
public static Image GetBarcodeImage(string barCodeValue, int width, int height) { if (barCodeValue.Length == 12) { barCodeValue += getUPCACheckDigit(barCodeValue); } com.google.zxing.oned.EAN13Writer writer = new com.google.zxing.oned.EAN13Writer(); com.google.zxing.common.ByteMatrix matrix = writer.encode(barCodeValue, com.google.zxing.BarcodeFormat.EAN_13, width, height); System.Drawing.Image drawingBarCode = matrix.ToBitmap(); // convert drawing to imageSource Image imageBarCode = new Image(); imageBarCode.Source = ImageHelper.DrawingImageToBitmapImage(drawingBarCode); return(imageBarCode); }