C#-QRCoder-二维码生成

0/准备Form

0-1/外观:

C#-QRCoder-二维码生成

 

 

 这里主要用到两个按键(button)、一个可多行的文本框(textBox)、一个图片框(pictureBox)

 

1/通过NuGet引入QRCoder

C#-QRCoder-二维码生成

 

 C#-QRCoder-二维码生成

 

 

按照上面步骤通过NuGet引入QRCoder包

 2/效果

C#-QRCoder-二维码生成

 

 

 

3/源码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using QRCoder;
11 
12 namespace testQRCode
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void Form1_Load(object sender, EventArgs e)
22         {
23             ;
24         }
25 
26         /// <summary>
27         /// 生成二维码
28         /// </summary>
29         /// <param name="msg">信息</param>
30         /// <param name="version">版本 1 ~ 40</param>
31         /// <param name="pixel">像素点大小</param>
32         /// <param name="icon_path">图标路径</param>
33         /// <param name="icon_size">图标尺寸</param>
34         /// <param name="icon_border">图标边框厚度</param>
35         /// <param name="white_edge">二维码白边</param>
36         /// <returns>位图</returns>
37 
38         //public static Bitmap code(string msg, int version, int pixel, string icon_path, int icon_size, int icon_border, bool white_edge)//带logo的
39         public static Bitmap code(string msg, int version, int pixel,bool white_edge)
40         {
41             QRCoder.QRCodeGenerator code_generator = new QRCoder.QRCodeGenerator();
42             QRCoder.QRCodeData code_data = code_generator.CreateQrCode(msg, QRCoder.QRCodeGenerator.ECCLevel.M, true, true, QRCoder.QRCodeGenerator.EciMode.Utf8, version);
43             QRCoder.QRCode code = new QRCoder.QRCode(code_data);
44             //Bitmap icon = new Bitmap(icon_path); //logo路径
45             Bitmap bmp = code.GetGraphic(pixel, Color.Black, Color.White, white_edge);
46 
47             return bmp;
48         }
49 
50         private void button1_Click(object sender, EventArgs e)
51         {
52             int version = Convert.ToInt16(10);//版本
53             int pixel = Convert.ToInt16(6);//像素
54             string str_msg = textBox1.Text;//文本(可以是网址链接)
55             //int int_icon_size = Convert.ToInt16(20);//logo尺寸
56             //int int_icon_border = Convert.ToInt16(5);////logo边线
57             bool b_we = true;
58 
59             Bitmap bmp = code(str_msg, version, pixel, b_we);
60 
61             pictureBox1.Image = bmp;
62         }
63 
64         private void button2_Click(object sender, EventArgs e)
65         {
66             if (pictureBox1.Image != null)
67             {
68                 using (SaveFileDialog sfd = new SaveFileDialog())
69                 {
70                     sfd.Filter = "(*.png)|*.png|(*.jpg)|*.jpg|(*.bmp)|*.bmp";
71 
72                     if (sfd.ShowDialog() == DialogResult.OK)
73                         pictureBox1.Image.Save(sfd.FileName);
74                 }
75             }
76         }
77     }
78 }

C#-QRCoder-二维码生成

上一篇:10.调用http api:引入protobuf、生成参数和响应模型


下一篇:C# List用法 -- 学习笔记