How to generate a QR code using. NET and a QR code using. NET

Source: Internet
Author: User

How to generate a QR code using. NET and a QR code using. NET

The following describes how to generate a QR Code using. NET. The detailed steps are as follows:

1. Create a window application and introduce the. net qr code library (open-source class library can be downloaded from the Internet ):

2. Build the UI with the following code:

Namespace WinQRCode {partial class Form1 {/// <summary> /// a required designer variable. /// </Summary> private System. ComponentModel. IContainer components = null; // <summary> /// clear all resources in use. /// </Summary> /// <param name = "disposing"> true if the managed resource should be released; otherwise, false. </Param> protected override void Dispose (bool disposing) {if (disposing & (components! = Null) {components. dispose ();} base. dispose (disposing );} # region Windows Form Designer generated code /// <summary> /// the designer supports the required methods-do not // use the code editor to modify the content of this method. /// </Summary> private void InitializeComponent () {this. button1 = new System. windows. forms. button (); this. button2 = new System. windows. forms. button (); this. pictureBox1 = new System. windows. forms. pictureBox (); this. label1 = new System. windows. forms. label (); this. textBox1 = new System. windows. forms. textBox (); this. label2 = new System. windows. forms. label (); this. lbl decoding information = new System. windows. forms. label (); (System. componentModel. ISupportInitialize) (this. pictureBox1 )). beginInit (); this. suspendLayout (); // button1 // this. button1.Location = new System. drawing. point (446, 31); this. button1.Margin = new System. windows. forms. padding (4, 5, 4, 5); this. button1.Name = "button1"; this. button1.Size = new System. drawing. size (103, 37); this. button1.TabIndex = 0; this. button1.Text = "encoding"; this. button1.UseVisualStyleBackColor = true; this. button1.Click + = new System. eventHandler (this. button#click); // button2 // this. button2.Location = new System. drawing. point (446, 81); this. button2.Margin = new System. windows. forms. padding (4, 5, 4, 5); this. button2.Name = "button2"; this. button2.Size = new System. drawing. size (103, 37); this. button2.TabIndex = 1; this. button2.Text = "decoding"; this. button2.UseVisualStyleBackColor = true; this. button2.Click + = new System. eventHandler (this. button2_Click); // pictureBox1 // this. pictureBox1.Location = new System. drawing. points (23,117); this. pictureBox1.Margin = new System. windows. forms. padding (4, 5, 4, 5); this. pictureBox1.Name = "pictureBox1"; this. pictureBox1.Size = new System. drawing. size (279,253); this. pictureBox1.SizeMode = System. windows. forms. pictureBoxSizeMode. stretchImage; this. pictureBox1.TabIndex = 2; this. pictureBox1.TabStop = false; // label1 // this. label1.AutoSize = true; this. label1.Location = new System. drawing. point (18, 39); this. label1.Margin = new System. windows. forms. padding (4, 0, 4, 0); this. label1.Name = "label1"; this. label1.Size = new System. drawing. size (69, 25); this. label1.TabIndex = 3; this. label1.Text = "information:"; // textBox1 // this. textBox1.Location = new System. drawing. point (95, 37); this. textBox1.Margin = new System. windows. forms. padding (4, 5, 4, 5); this. textBox1.Name = "textBox1"; this. textBox1.Size = new System. drawing. size (333, 31); this. textBox1.TabIndex = 4; // label2 // this. label2.AutoSize = true; this. label2.Location = new System. drawing. point (18, 81); this. label2.Margin = new System. windows. forms. padding (4, 0, 4, 0); this. label2.Name = "label2"; this. label2.Size = new System. drawing. size (88, 25); this. label2.TabIndex = 5; this. label2.Text = "QR code:"; // lbl decoding information // this. lbl decoding information. autoSize = true; this. lbl decoding information. location = new System. drawing. point (101, 81); this. lbl decoding information. margin = new System. windows. forms. padding (4, 0, 4, 0); this. lbl decoding information. name = "lbl decoding information"; this. lbl decoding information. size = new System. drawing. size (0, 25); this. lbl decoding information. tabIndex = 6; // Form1 // this. autoScaleDimensions = new System. drawing. sizeF (11F, 24F); this. autoScaleMode = System. windows. forms. autoScaleMode. font; this. clientSize = new System. drawing. size (572,384); this. controls. add (this. lbl decoding information); this. controls. add (this. label2); this. controls. add (this. textBox1); this. controls. add (this. label1); this. controls. add (this. pictureBox1); this. controls. add (this. button2); this. controls. add (this. button1); this. font = new System. drawing. font ("", 10.8F, System. drawing. fontStyle. regular, System. drawing. graphicsUnit. point, (byte) (134); this. margin = new System. windows. forms. padding (4, 5, 4, 5); this. name = "Form1"; this. text = "QR code"; (System. componentModel. ISupportInitialize) (this. pictureBox1 )). endInit (); this. resumeLayout (false); this. extends mlayout () ;}# endregion private System. windows. forms. button button1; private System. windows. forms. button button2; private System. windows. forms. pictureBox pictureBox1; private System. windows. forms. label label1; private System. windows. forms. textBox textBox1; private System. windows. forms. label label2; private System. windows. forms. label lbl decoding information ;}}

The figure is as follows:

 

3. Write encoding and decoding button events for encoding and decoding:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace WinQRCode {using ThoughtWorks. QRCode. codec; using ThoughtWorks. QRCode. codec. data; using ThoughtWorks. QRCode. codec. util; using System. IO; using implements toimage; public partial class Form1: Form {public Form1 () {InitializeComponent ();} /// <summary> /// encoding /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void button#click (object sender, eventArgs e) {if (this. textBox1.Text! = "") {Cursor. current = Cursors. waitCursor; if (textBox1.Text. trim () = String. empty) {MessageBox. show ("cannot be blank. "); return;} QRCodeEncoder qrCodeEncoder = new QRCodeEncoder (); qrCodeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.BYTE; qrCodeEncoder. QRCodeScale = 4; qrCodeEncoder. QRCodeVersion = 7; qrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M; System. drawing. image image; String data = textBox1.Text; // encoded image = qrCodeEncoder. encode (data); // The image displays this. pictureBox1.Image = image; Cursor. current = Cursors. default ;}} /// <summary> /// decoding /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void button2_Click (object sender, eventArgs e) {Cursor. current = Cursors. waitCursor; Bitmap B = this. pictureBox1.Image as Bitmap; try {QRCodeDecoder decoder = new QRCodeDecoder (); // decodes String decodedString = decoder. decode (new QRCodeBitmapImage (B); // display the decoding information this. lbl decoding information. text + = decodedString;} catch (Exception ex) {MessageBox. show (ex. message);} Cursor. current = Cursors. default;} private void Form1_Load (object sender, EventArgs e ){}}}

4. Compile and run the program, for example:

The above is the general idea of using. NET to generate a QR code. There are still many shortcomings. I hope you can give more suggestions or make innovative modifications on your own.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.