Asp. NET generation of QR code examples

Source: Internet
Author: User
Tags add time save file

Share an example of C # to generate two-dimensional code, directly referencing the ThoughtWorks.QRCode.dll class to generate two-dimensional code, there is a need for friends reference.

Method 1, directly refer to the ThoughtWorks.QRCode.dll class, generate two-dimensional code.

code example:

ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder =NewQrcodeencoder (); encoder. Qrcodeencodemode= Qrcodeencoder.encode_mode. BYTE;//encoding Method (Note: Byte can support Chinese, alpha_numeric scan out is the number)Encoder. Qrcodescale =4;//sizeEncoder. Qrcodeversion =0;//Version (note: setting to 0 is primarily to prevent an error when the encoded string is too long)Encoder. Qrcodeerrorcorrect =qrcodeencoder.error_correction. M String Qrdata="Two-dimensional code information"; System.Drawing.Bitmap BP= Encoder. Encode (Qrdata. ToString (), Encoding.GetEncoding ("GB2312")); Image Image=bp;object omissing=System.reflection.missing.value;picturebox1.image= BP;

Save two-dimensional code image:

code example:

SaveFileDialog SF =NewSaveFileDialog (); sf. Title="Select Save File location"; sf. Filter="Save Picture (*.jpg) |*.jpg| All Files (*. *) |*.*";//Set Default file type display ordersf. FilterIndex =1;//Save dialog box remembers the last Open directorysf. Restoredirectory =true;if(SF. ShowDialog () = =DialogResult.OK) {Image im= This. pictureBox1.Image; //Get file path    stringLocalfilepath =SF.    Filename.tostring (); if(SF. FileName! ="")    {       stringFilenameext = localfilepath.substring (Localfilepath.lastindexof ("\\") +1);//get the file name without a path//NewFileName = filenameext+datetime.now.tostring ("YyyyMMdd");//add time to file name       stringFilePath = localfilepath.substring (0, Localfilepath.lastindexof ("."));//get file path, with file name, without suffix        stringfn =SF.        FileName; PictureBox1.Image.Save (FilePath+"-"+ DateTime.Now.ToString ("YYYYMMDD") +". jpg"); }}//parsing Two-dimensional code information//Qrcodedecoder decoder = new Qrcodedecoder ();//String decodedstring = Decoder.decode (new Qrcodebitmapimage (New Bitmap (pictureBox1.Image)));//this.label3.Text = decodedstring;

Method 2, reference the Zxing class library.
Zxing is an open source Java class Library for parsing 1d/2d barcodes in a variety of formats. The goal is to decode the QR code, Data Matrix, and UPC 1D barcodes. At the same time, it also provides a class library such as cpp,actionscript,android,iphone,rim,j2me,j2se,jruby,c#. The role of the Zxing class library is mainly decoding, is the current open source class library decoding ability is relatively strong (commercial, but for thousands of class library licensing fees, it is really worth).
To Google code to download the corresponding codes
1. Download the latest zxing package
to Zxing's homepage: http://code.google.com/p/zxing/
Locate the CSharp folder, open it in VS and compile it, and copy and paste the Zxing.dll from obj under debug into the bin file directory in your project.
Right-click to add a project reference. By referencing Zxing.dll to your project, you can use it where you need it.
There are two UTF-8 problems in the source code, which can result in garbled Chinese (modified before compiling the. dll)
One: in the Com.google.zxing.qrcode.encoder.encoder class
Internal const System.String default_byte_mode_encoding = "Iso-8859-1";
Here, change iso-8859-1 to UTF-8
Second: Members of the Com.google.zxing.qrcode.decoder.DecodedBitStreamParser class
Private Const System.String UTF8 = "UTF8";
The UTF8 should be changed to UTF-8

code example:

usingCom.google.zxing.qrcode;usingcom.google.zxing;usingCom.google.zxing.common;usingBytematrix =Com.google.zxing.common.ByteMatrix;usingEan13writer =Com.google.zxing.oned.EAN13Writer;usingEan8writer =Com.google.zxing.oned.EAN8Writer;usingMultiformatwriter =Com.google.zxing.MultiFormatWriter; Method:stringContent ="Two-dimensional code information"; Bytematrix Bytematrix=NewMultiformatwriter (). Encode (content, Barcodeformat.qr_code, -, -); Bitmap Bitmap=Tobitmap (Bytematrix); pictureBox1.Image=bitmap; SaveFileDialog SFD=NewSaveFileDialog (); Sfd.filter="Save Picture (*.png) |*.png| All Files (*. *) |*.*"; Sfd.defaultext="*.png|*.png"; Sfd.addextension=true; if(Sfd.showdialog () = =DialogResult.OK) {if(Sfd.filename! =""{writetofile (Bytematrix, System.Drawing.Imaging.ImageFormat.Png, Sfd.filename);}}


Parsing two-dimensional code:

code example:

 if( This. Openfiledialog1.showdialog ()! =DialogResult.OK) {return; } Image img= Image.FromFile ( This. Openfiledialog1.filename);        Bitmap Bmap; Try{Bmap=NewBitmap (IMG); }        Catch(System.IO.IOException IoE) {MessageBox.Show (IoE. ToString ());return; }        if(Bmap = =NULL) {MessageBox.Show ("Could not decode image");return; } Luminancesource Source=NewRgbluminancesource (Bmap, Bmap. Width, Bmap.        Height); Com.google.zxing.BinaryBitmap Bitmap1=NewCom.google.zxing.BinaryBitmap (NewHybridbinarizer (source));        result result; Try{result=NewMultiformatreader (). Decode (BITMAP1); }        Catch(Readerexception re) {MessageBox.Show (re). ToString ());return; } MessageBox.Show (Result. Text);  Public Static voidWriteToFile (Bytematrix Matrix, System.Drawing.Imaging.ImageFormat format,stringfile) {Bitmap Bmap=Tobitmap (matrix); Bmap.    Save (file, format); }     Public StaticBitmap Tobitmap (Bytematrix matrix) {intwidth =Matrix.        Width; intHeight =Matrix.        Height; Bitmap Bmap=NewBitmap (width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);  for(intx =0; x < width; X + +)        { for(inty =0; Y < height; y++) {Bmap. SetPixel (x, Y, matrix.get_renamed (x, y)!= -1? Colortranslator.fromhtml ("0xff000000"): Colortranslator.fromhtml ("0xFFFFFFFF"));} }        returnBmap; }

Asp. NET generation of QR code examples

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.