下载Zxing.net官网:https://archive.codeplex.com/?p=zxingnet
或者去VS程序包下载

封装好的代码:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Drawing;
7 using ZXing;
8 using ZXing.Common;
9 using ZXing.QrCode;
10 using ZXing.QrCode.Internal;
11 using System.Drawing.Imaging;
12
13 namespace WindowsFormsApplication1
14 {
15 public class BarMethHepler
16 {
17 /// <summary>
18 /// 生成二维码
19 /// </summary>
20 /// <param name="text">内容</param>
21 /// <param name="width">宽度</param>
22 /// <param name="height">高度</param>
23 /// <returns></returns>
24 public static Bitmap Generate1(string text, int width, int height)
25 {
26 BarcodeWriter writer = new BarcodeWriter();
27 writer.Format = BarcodeFormat.QR_CODE;
28 QrCodeEncodingOptions options = new QrCodeEncodingOptions()
29 {
30 DisableECI = true,//设置内容编码
31 CharacterSet = "UTF-8", //设置二维码的宽度和高度
32 Width = width,
33 Height = height,
34 Margin = 1//设置二维码的边距,单位不是固定像素
35 };
36
37 writer.Options = options;
38 Bitmap map = writer.Write(text);
39 return map;
40 }
41
42 /// <summary>
43 /// 生成一维条形码
44 /// </summary>
45 /// <param name="text">内容</param>
46 /// <param name="width">宽度</param>
47 /// <param name="height">高度</param>
48 /// <returns></returns>
49 public static Bitmap Generate2(string text, int width, int height)
50 {
51 BarcodeWriter writer = new BarcodeWriter();
52 //使用ITF 格式,不能被现在常用的支付宝、微信扫出来
53 //如果想生成可识别的可以使用 CODE_128 格式
54 //writer.Format = BarcodeFormat.ITF;
55 writer.Format = BarcodeFormat.CODE_39;
56 EncodingOptions options = new EncodingOptions()
57 {
58 Width = width,
59 Height = height,
60 Margin = 2
61 };
62 writer.Options = options;
63 Bitmap map = writer.Write(text);
64 return map;
65 }
66
67 /// <summary>
68 /// 生成带Logo的二维码
69 /// </summary>
70 /// <param name="text">内容</param>
71 /// <param name="width">宽度</param>
72 /// <param name="height">高度</param>
73 public static Bitmap Generate3(string text, int width, int height)
74 {
75 //Logo 图片
76 string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.png";
77 Bitmap logo = new Bitmap(logoPath);
78 //构造二维码写码器
79 MultiFormatWriter writer = new MultiFormatWriter();
80 Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
81 hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
82 hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
83 //hint.Add(EncodeHintType.MARGIN, 2);//旧版本不起作用,需要手动去除白边
84
85 //生成二维码
86 BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width + 30, height + 30, hint);
87 bm = deleteWhite(bm);
88 BarcodeWriter barcodeWriter = new BarcodeWriter();
89 Bitmap map = barcodeWriter.Write(bm);
90
91 //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
92 int[] rectangle = bm.getEnclosingRectangle();
93
94 //计算插入图片的大小和位置
95 int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width);
96 int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height);
97 int middleL = (map.Width - middleW) / 2;
98 int middleT = (map.Height - middleH) / 2;
99
100 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
101 using (Graphics g = Graphics.FromImage(bmpimg))
102 {
103 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
104 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
105 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
106 g.DrawImage(map, 0, 0, width, height);
107 //白底将二维码插入图片
108 g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
109 g.DrawImage(logo, middleL, middleT, middleW, middleH);
110 }
111 return bmpimg;
112 }
113
114 /// <summary>
115 /// 删除默认对应的空白
116 /// </summary>
117 /// <param name="matrix"></param>
118 /// <returns></returns>
119 private static BitMatrix deleteWhite(BitMatrix matrix)
120 {
121 int[] rec = matrix.getEnclosingRectangle();
122 int resWidth = rec[2] + 1;
123 int resHeight = rec[3] + 1;
124
125 BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
126 resMatrix.clear();
127 for (int i = 0; i < resWidth; i++)
128 {
129 for (int j = 0; j < resHeight; j++)
130 {
131 if (matrix[i + rec[0], j + rec[1]])
132 resMatrix[i, j] = true;
133 }
134 }
135 return resMatrix;
136 }
137 }
138 }
效果图:

转载
作者:Alan.hsiang
出处:http://www.cnblogs.com/hsiang/
来源:https://www.cnblogs.com/yinyongle/p/10564917.html