赞
踩
1.主要是记录一下,可能以后自己能再次通过自己写的这篇东西温故而知新;
其中参考了几篇其他大神写的实现c#二维码之代码。
首先c#要想实现二维码的相关操作需要ZXing.dll这个文件
具体方式如下
项目中右键找到管理NuGet程序包(N)
安装成功后;进行如下代码
方式1.通过创建类的方式,将系统提供的ZXing类的数据进行封装
先创建类ZXingLibs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using System.Drawing.Imaging;
- using ZXing;
- using ZXing.Common;
- using ZXing.QrCode;
- using ZXing.QrCode.Internal;
-
-
-
- namespace WindowsFormsApp2
- {
- class ZXingLibs
- {
- /// 生成二维码
- public static Bitmap GetQRCode(string text, int width, int height)
- {
- BarcodeWriter writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.QR_CODE;
- QrCodeEncodingOptions options = new QrCodeEncodingOptions()
- {
- DisableECI = true,//设置内容编码
- CharacterSet = "UTF-8",
- Width = width,//设置二维码的宽度和高度
- Height = height,
- Margin = 1//设置二维码的边距,单位不是固定像素
- };
-
- writer.Options = options;
- Bitmap map = writer.Write(text);
- return map;
- }
-
- /// 生成一维条形码
- public static Bitmap GetBarCode(string text, int width, int height)
- {
- BarcodeWriter writer = new BarcodeWriter();
- //使用ITF 格式,不能被现在常用的支付宝、微信扫出来
- //如果想生成可识别的可以使用 CODE_128 格式
- //writer.Format = BarcodeFormat.ITF;
- writer.Format = BarcodeFormat.CODE_39;
- EncodingOptions options = new EncodingOptions()
- {
- Width = width,
- Height = height,
- Margin = 2
- };
- writer.Options = options;
- Bitmap map = writer.Write(text);
- return map;
- }
-
- /// 生成带Logo的二维码
- public static Bitmap GetQRCodeWithLogo(string text, int width, int height)
- {
- //Logo 图片
- string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"zp.bmp";
- Bitmap logo = new Bitmap(logoPath);
- //构造二维码写码器
- MultiFormatWriter writer = new MultiFormatWriter();
- Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
- hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
- hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
- //hint.Add(EncodeHintType.MARGIN, 2);//旧版本不起作用,需要手动去除白边
-
- //生成二维码
- BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width + 80, height + 80, hint);
- bm = deleteWhite(bm);
- BarcodeWriter barcodeWriter = new BarcodeWriter();
- Bitmap map = barcodeWriter.Write(bm);
-
- //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
- int[] rectangle = bm.getEnclosingRectangle();
-
- //计算插入图片的大小和位置
- int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
- int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
- int middleL = (map.Width - middleW) / 2;
- int middleT = (map.Height - middleH) / 2;
-
- Bitmap bmpimg = new Bitmap(map.Width, map.Height, 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(map, 0, 0, width, height);
- //白底将二维码插入图片
- g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
- g.DrawImage(logo, middleL, middleT, middleW, middleH);
- }
- return bmpimg;
- }
-
- /// 删除默认对应的空白
- private static BitMatrix deleteWhite(BitMatrix matrix)
- {
- int[] rec = matrix.getEnclosingRectangle();
- int resWidth = rec[2] + 1;
- int resHeight = rec[3] + 1;
-
- BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
- resMatrix.clear();
- for (int i = 0; i < resWidth; i++)
- {
- for (int j = 0; j < resHeight; j++)
- {
- if (matrix[i + rec[0], j + rec[1]])
- resMatrix[i, j] = true;
- }
- }
- return resMatrix;
- }
- }
- }
再通过调用ZXingLibs类的方式进行生成二维码,如下图所示
- private void Button10_Click(object sender, EventArgs e)
- {
- string qrData = "";
- qrData = "" + textBox1.Text ;
- pictureBox1.Image = ZXingLibs.GetQRCode(qrData, pictureBox1.Width, pictureBox1.Height);
-
-
- }
方式二:直接利用ZXing中自带的类,直接调用;相对来说直接使用时,没有方式1(已二次封装)更加简洁
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using ZXing.Common;
- using ZXing;
- namespace WindowsFormsApp2
- {
- public partial class FrmUseControls : Form
- {
- public FrmUseControls()
- {
- InitializeComponent();
- }
-
- private void Button11_Click(object sender, EventArgs e)
- {
- // 1.设置QR二维码的规格
- ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions();
- qrEncodeOption.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码
- qrEncodeOption.Height = pictureBox1.Height;//设置高度
- qrEncodeOption.Width = pictureBox1.Width; //设置宽度
- qrEncodeOption.Margin = 1; // 设置周围空白边距
-
- // 2.生成条形码图片并保存
- ZXing.BarcodeWriter Qr = new BarcodeWriter();
- Qr.Format = BarcodeFormat.QR_CODE; // 二维码
- Qr.Options = qrEncodeOption;
- Bitmap img = Qr.Write(textBox1.Text);//"textBox1"就是用来输入数据内容的控件
- pictureBox1.Image = img;
-
-
-
-
-
-
-
-
-
-
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。