VS2015 VB.Net利用QrCodeNet生成QR Code

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

Step by step Create QR Code with QrCodeNet

Step.1 新建項目

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

Step.3 加入參考(引用)

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

Step.5 添中 Imports

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

Step.6 粘貼代碼

代码语言:javascript复制
 1 Public Class Form1 : Inherits Form
 2 
 3     ' // 定義常量
 4     Public Const str = "160-12345678-8888,AbcdefgHijklmnOpqRst,2018/05/22.160-12345678-8888,AbcdefgHijklmnOpqRst,2018/05/22."
 5 
 6     ' // 重寫 OnPaint 方法,增加繪制二維碼方法調用
 7     Protected Overrides Sub OnPaint(e As PaintEventArgs)
 8         MyBase.OnPaint(e)
 9 
10         ' // 繪制二維碼
11         Call ShowQrCode(e.Graphics)
12 
13         tbStrContent.Text = str
14 
15     End Sub
16 
17     ' // Save File 按鈕事件
18     Private Sub btnSaveFile_Click(sender As Object, e As EventArgs) Handles btnSaveFile.Click
19 
20         Const moduleSizeInPixels = 4
21 
22         Dim qrEncoder As New QrEncoder(ErrorCorrectionLevel.L)
23         Dim qrCode As New QrCode()
24 
25         Dim moduleSize As New FixedModuleSize(moduleSizeInPixels, QuietZoneModules.Two)
26         Dim render As New GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White)
27 
28         Dim fileName As String = Application.ExecutablePath   "_One.png"
29 
30         ' // 錯誤繼續執行
31         On Error Resume Next
32 
33         ' // 根據字符串生成 QR Code
34         qrEncoder.TryEncode(str, qrCode)
35 
36         ' // 寫入文件
37         Using stream As New FileStream(fileName, FileMode.Create)
38             render.WriteToStream(qrCode.Matrix, System.Drawing.Imaging.ImageFormat.Png, stream)
39         End Using
40 
41     End Sub
42 
43     ' // 繪制二維碼
44     Private Sub ShowQrCode(g As Graphics)
45         Dim qrEncoder As New QrEncoder(ErrorCorrectionLevel.L)
46         Dim qrCode As New QrCode()
47 
48         Dim moduleSize As New FixedModuleSize(2, QuietZoneModules.Two)
49         Dim render As New GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White)
50 
51         ' // 根據字符串生成 QR Code
52         qrCode = qrEncoder.Encode(str)
53 
54         ' // 描繪 QR Code
55         render.Draw(g, qrCode.Matrix)
56 
57     End Sub
58 
59 End Class

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 人点赞