VS2015 C#利用QrCodeNet生成QR Code

2024-08-14 15:29:10 浏览数 (1)

Step by step Create QR Code with QrCodeNet

Step.1 新建項目

Step.2 在窗口中拖入一個Button

Step.3 下載QrCodeNet代碼,解壓QrCodeNetsourceCodeqrcodenetGma.QrCodeNetnugetlibnet45Gma.QrCodeNet.Encoding.dll

Step.4 加入參考(引用)

Step.5 添中using

代码语言:javascript复制
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.IO;

Step.6 粘貼代碼

代码语言:javascript复制
 1 public partial class Form1 : Form
 2     {
 3         const string str = "160-12345678-8888,AbcdefgHijklmnOpqRst,2018/05/21";
 4         public Form1()
 5         {
 6             InitializeComponent();
 7         }
 8         protected override void OnPaint(PaintEventArgs e)
 9         {
10             base.OnPaint(e);
11             ShowCode(e.Graphics);
12         }
13         private void ShowCode(Graphics g)
14         {
15             QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L);
16             QrCode qrCode = qrEncoder.Encode(str);
17 
18             FixedModuleSize moduleSize = new FixedModuleSize(2, QuietZoneModules.Two);
19             GraphicsRenderer render = new GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White);
20             render.Draw(g, qrCode.Matrix);
21         }
22 
23         private void btnSaveFile_Click(object sender, EventArgs e)
24         {
25             QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L);
26             QrCode code = new QrCode();
27             qrEncoder.TryEncode(str, out code);
28 
29             const int modelSizeInPixels = 4;
30 
31             GraphicsRenderer render = new GraphicsRenderer(
32                 new FixedModuleSize(modelSizeInPixels, QuietZoneModules.Two),
33                 Brushes.Black,
34                 Brushes.White);
35 
36             string fileName = Application.ExecutablePath   "New.png";
37 
38             using (FileStream stream = new FileStream(fileName, FileMode.Create))
39             {
40                 render.WriteToStream(code.Matrix, System.Drawing.Imaging.ImageFormat.Png, stream);
41             }
42         }
43     }

Step.7 運行結果

參考資料:

QR Code

http://www.qrcode.com/zh/about/

C#利用QrCode.Net生成二维码(Qr码)

https://www.cnblogs.com/tianma3798/p/4054270.html

利用QrCode.Net生成二维码 asp.net mvc c#

https://blog.csdn.net/paolei/article/details/12584295

草料二维码

https://cli.im/

VB.Net创建和调用DLL教程

https://blog.csdn.net/namecgc/article/details/77940698

0 人点赞