Do not know that we have to make more than two-dimensional code? This article I will show you how to use C # to generate two-dimensional code, first of all, to generate two-dimensional code needs three classes Qrcodegenerator, Qrcodedata, QRCode. For the back good understanding. What are their responsibilities? Qrcodegenerator: A data object used to generate two-dimensional code stored in a specified way, that is, the Matrix in the middle of the Qrcodedata QR code, after which QRCode gets qrcodedata and generates a two-dimensional code
Two-dimensional code
1. Preface
Recently in the two-dimensional code in terms of some things, so contact with some QR code correlation, then since the use of the need to be useful after the appearance of
In fact, about the two-dimensional code of the article is really numerous, there are a lot of writing is very serious, very good, but it is like learning, others will not represent themselves without learning and record the value, so that learning is not in the morning and night
Introduction Package
1. Introducing Qrcoder through NuGet
A) First, we create a new class Library project, which is named Chestnut_qrcode
b) After the introduction of the Qrcoder package via NuGet
c) See figure operation
D) A Qrcoder reference will appear in the project reference after the installation is successful
E) This time the introduction work has been completed, but can easily create a Encoder.cs静态公共类
Two-dimensional code generation class
1. Pre-preparation
Here, let's start with the three classes required to generate the QR code:
Qrcodegenerator
Qrcodedata
QRCode
What are their responsibilities ?
qrcodegenerator : A data object used to generate a QR code store in a specified way, i.e. the Matrixin the middle of the qrcodedata QR code, after which QRCode gets Qrcodedata and generate two-dimensional code
2. Encoding
Encoder.cs All codes are as follows:
Using System.drawing;namespace chestnut_qrcode{//<summary>///QR Code encoder///</summary> public STA Tic class Encoder {//<summary>//Generate QR Code///</summary>//<param name= " MSG "> Information </param>//<param name=" Version "> Release 1 ~ 40</param>//<param name=" pixel "& gt; Pixel point size </param>//<param name= "Icon_path" > Icon path </param>//<param name= "Icon_size" &G t; icon size </param>///<param Name= "Icon_border" > icon Border thickness </param>//<param name= "White_edge > QR code white Edge </param>//<returns> bitmap </returns> public static Bitmap code (string msg, int ver sion, int pixel, string icon_path, int icon_size, int icon_border, bool White_edge) {Qrcoder.qrcodegene Rator code_generator = new Qrcoder.qrcodegenerator (); Qrcoder.qrcodedata code_data = Code_generator. Createqrcode (MSG, Qrcoder.qrcodegenErator. ecclevel.m/* here sets a level of fault tolerance */, True, True, QRCoder.QRCodeGenerator.EciMode.Utf8, version); Qrcoder.qrcode code = new Qrcoder.qrcode (code_data); Bitmap icon = new Bitmap (Icon_path); Bitmap BMP = code. Getgraphic (Pixel, Color.Black, Color.White, icon, Icon_size, Icon_border, White_edge); return BMP; } }}
The parameters between the fault tolerance and version are related to the encoding format, and some formats do not support Chinese.
Prepare form
1. Appearance
Here Seaconch is using the WinForm project, just cut a map.
2. Code
Using system;using system.drawing;using system.windows.forms;namespace chestnut_form{public partial class Frm_qrcode: Form {public Frm_qrcode () {InitializeComponent (); }//The form loads private void Frm_qrcode_load (object sender, EventArgs e) {cb_version. SelectedIndex = 1; Cb_pixel. SelectedIndex = 0; Cb_icon_size. SelectedIndex = 0; Cb_icon_border. SelectedIndex = 1; }//code private void Btn_encode_click (object sender, EventArgs e) {int version = Convert. ToInt16 (cb_version. Text); int pixel = convert.toint16 (cb_pixel. Text); String str_msg = Tb_msg. Text; int int_icon_size = Convert.ToInt16 (cb_icon_size. Text); int int_icon_border = Convert.ToInt16 (cb_icon_border. Text); BOOL B_we = rb_we_y.checked? True:false; Bitmap bmp = Chestnut_qrcode. Encoder.code (str_msg, version, pixel, "E:/seaconCh/git/1.jpg ", Int_icon_size, Int_icon_border, b_we); Pb_qrcode. Image = BMP; }//Save private void Btn_save_click (object sender, EventArgs e) {if (Pb_qrcode. Image! = null) using (SaveFileDialog SFD = new SaveFileDialog ()) {sfd. Filter = "(*.png) |*.png| (*.bmp) |*.bmp "; if (SFD. ShowDialog () = = DialogResult.OK) pb_qrcode. Image.Save (SFD). FileName); } } }}
Look at the C # QR code generation effect
Related articles:
C # Tutorial C # data type
Related videos:
Geek College C # video tutorial