C # implements two-dimensional code functions, WinForm and ASP.

Source: Internet
Author: User

QR code now uses a lot, how to use C # programming to generate it?

Prepare ThoughtWorks.QRCode.dll

Requires a class QRCode, which is specifically designed to generate two-dimensional code

usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingThoughtworks.qrcode;usingThoughtWorks.QRCode.Codec;namespaceqrcodepro{ Public classQRCode {///generate two-dimensional code/// </summary>        /// <param name= "Strdata" >the text or number to be generated</param>        /// <param name= "qrencoding" >Three sizes: BYTE, Alpha_numeric,numeric</param>        /// <param name= "level" >size: L M Q H</param>        /// <param name= "version" >version: if 8</param>        /// <param name= "scale" >scale: If 4</param>        /// <returns></returns>         Public StaticImage Createcode_choose (stringStrdata,stringQrencoding,stringSizeintVersionintScale ) {Qrcodeencoder Qrcodeencoder=NewQrcodeencoder (); stringencoding =qrencoding; Switch(encoding) { Case "Byte": Qrcodeencoder.qrcodeencodemode=Qrcodeencoder.encode_mode.                    BYTE;  Break;  Case "alphanumeric": Qrcodeencoder.qrcodeencodemode=Qrcodeencoder.encode_mode.                    Alpha_numeric;  Break;  Case "Numeric": Qrcodeencoder.qrcodeencodemode=Qrcodeencoder.encode_mode.                    NUMERIC;  Break; default: Qrcodeencoder.qrcodeencodemode=Qrcodeencoder.encode_mode.                    BYTE;  Break; } Qrcodeencoder.qrcodescale=Scale ; Qrcodeencoder.qrcodeversion=version; Switch(size) { Case "L": Qrcodeencoder.qrcodeerrorcorrect=qrcodeencoder.error_correction.                    L  Break;  Case "M": Qrcodeencoder.qrcodeerrorcorrect=qrcodeencoder.error_correction.                    M  Break;  Case "Q": Qrcodeencoder.qrcodeerrorcorrect=qrcodeencoder.error_correction.                    Q;  Break; default: Qrcodeencoder.qrcodeerrorcorrect=qrcodeencoder.error_correction.                    H  Break; }            //text Generation PicturesImage image =Qrcodeencoder.encode (strdata); returnimage; }    }}

Create a new form in VS, build the form:

Code required in the main form

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceqrcodepro{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e) {CBOENCODING.ITEMS.ADD ("Byte"); CBOENCODING.ITEMS.ADD ("alphanumeric"); CBOENCODING.ITEMS.ADD ("Numeric"); Cboencoding.selectedindex=0; //Fill Size drop-down boxCBOSIZE.ITEMS.ADD ("M"); CBOSIZE.ITEMS.ADD ("L"); CBOSIZE.ITEMS.ADD ("Q"); CBOSIZE.ITEMS.ADD ("H"); Cbosize.selectedindex=0; //Fill text BoxTxtversion.text ="8"; Txtscale.text="4"; }        Private voidBtncreatecode_click (Objectsender, EventArgs e) {            stringContent =Txtcontent.text; intVersion =Convert.ToInt32 (Txtversion.text); intScale =Convert.ToInt32 (Txtscale.text); stringSize =cboSize.SelectedItem.ToString (); stringencoding =cboEncoding.SelectedItem.ToString (); //check null value string            if(Content = =NULL|| Content = ="") {MessageBox.Show ("Please input QR code information, support Chinese"); }            Else{Image img=qrcode.createcode_choose (content, encoding, size, version, scale); Piccode.image=img; }        }        Private voidBtnreset_click (Objectsender, EventArgs e) {Txtcontent.text=""; }        Private voidBtnsave_click (Objectsender, EventArgs e) {            if(Piccode.image! =NULL) {saveimages (Piccode); }            Else{MessageBox.Show ("We haven't generated the image yet! "); }        }        Private voidsaveimages (PictureBox pic) {stringParentpath =@"D:\Photo"; //number of milliseconds since January 1, 1970 00:00:00 GMT            stringfilename =DateTime.Now.Ticks.ToString (); if(Parentpath.substring (Parentpath.length-1,1) !=@"\") Parentpath= Parentpath +@"\"; stringChildpath = Parentpath + DateTime.Now.ToString ("YYYYMMDD"); if(!directory.exists (Childpath))            Directory.CreateDirectory (Childpath); Pic. Image.Save (Childpath+"\\"+ filename +". jpg", System.Drawing.Imaging.ImageFormat.Jpeg); MessageBox.Show ("Save success! "); }    }}

The effect of the implementation

Keep files

Porting this project to an ASP. NET site is also extremely straightforward and is no longer demonstrated here.

C # implements two-dimensional code functions, WinForm and ASP.

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.