Asp. NET generation of two-dimensional code method summary _ Practical skills

Source: Internet
Author: User
Tags add time datetime save file

The method of generating two-dimensional code by ASP.net is summarized in this paper. Share to everyone for your reference, specific as follows:

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

Method 1. Directly referencing the ThoughtWorks.QRCode.dll class to generate a two-dimensional code.

code example:

ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new Qrcodeencoder ();
Encoder. Qrcodeencodemode = Qrcodeencoder.encode_mode. byte;//encoding Method (Note: Byte can support Chinese, alpha_numeric scan is the number)
encoder. Qrcodescale = 4;//size
encoder. Qrcodeversion = 0;//Version (note: setting to 0 is primarily to prevent errors that occur 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 picture:

code example:

 SaveFileDialog SF = new SaveFileDialog (), SF.
Title = "Choose Save File Location"; sf.
Filter = "Save Picture (*.jpg) |*.jpg| All Files (*.*) |*.*"; Sets the default file type display order SF.
FilterIndex = 1; Saves the dialog box to remember the last Open Directory SF.
Restoredirectory = true; if (SF.
  ShowDialog () = = DialogResult.OK) {Image im = This.pictureBox1.Image; Get file path string localfilepath = SF.
  Filename.tostring (); if (SF.
    FileName!= "") {string filenameext = Localfilepath.substring (Localfilepath.lastindexof ("\") + 1)//get filename without path NewFileName = filenameext+datetime.now.tostring ("YYYYMMDD")//Add time string to filename FilePath = localfilepath.substring (0, Localfilepath.lastindexof (".")); Gets the file path, with the filename, without the suffix string fn = sf.
    FileName;
  PictureBox1.Image.Save (FilePath + "-" + DateTime.Now.ToString ("yyyyMMdd") + ". jpg");
}///Parse 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 multiple formats. The goal is to decode the QR code, Data Matrix, UPC 1D Barcode. At the same time, it also provides class libraries such as cpp,actionscript,android,iphone,rim,j2me,j2se,jruby,c#. Zxing class Library function is mainly decoding, is currently open source class library decoding ability is relatively strong (business, but for the tens of thousands of class library licensing costs, indeed very value).

To Google code to download the corresponding codes

1. Download Zxing Latest Package

To Zxing's homepage: http://code.google.com/p/zxing/

Locate the CSharp folder, open and compile in VS, and copy and paste the Zxing.dll in debug under obj to the bin file directory in your project.
Right-click to add a project reference. Refer Zxing.dll to your project and you can use it where you need it.

There are two UTF-8 problems in the source code that can cause Chinese to be garbled (modified before compiling. dll)

One: 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:

Using Com.google.zxing.qrcode;
Using Com.google.zxing;
Using Com.google.zxing.common;
Using Bytematrix = Com.google.zxing.common.ByteMatrix;
Using Ean13writer = Com.google.zxing.oned.EAN13Writer;
Using Ean8writer = Com.google.zxing.oned.EAN8Writer;
Using Multiformatwriter = Com.google.zxing.MultiFormatWriter;

Method:

String content = "Two-dimensional code information";
Bytematrix Bytematrix = new Multiformatwriter (). Encode (content, Barcodeformat.qr_code, MB, MB);
Bitmap Bitmap = Tobitmap (Bytematrix);
pictureBox1.Image = bitmap;
SaveFileDialog SFD = new SaveFileDialog ();
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);
}


Parse two-dimensional code:

code example:

if (This.openFileDialog1.ShowDialog ()!= DialogResult.OK) {return;}
Image img = image.fromfile (this.openFileDialog1.FileName);
Bitmap Bmap; try {bmap = new Bitmap (IMG);} catch (System.IO.IOException IoE) {MessageBox.Show (IoE.
ToString ());
Return
} if (bmap = = null) {MessageBox.Show ("Could not decode image"); Luminancesource Source = new Rgbluminancesource (Bmap, Bmap. Width, Bmap.
Height);
Com.google.zxing.BinaryBitmap bitmap1 = new Com.google.zxing.BinaryBitmap (new Hybridbinarizer (source));
result result; try {result = new Multiformatreader (). Decode (BITMAP1);} catch (Readerexception re) {MessageBox.Show (re.
ToString ());
Return } MessageBox.Show (Result.
Text); public static void WriteToFile (Bytematrix matrix, System.Drawing.Imaging.ImageFormat format, string file) {Bitmap BMA
    p = tobitmap (matrix); Bmap.
Save (file, format); public static Bitmap Tobitmap (Bytematrix matrix) {int width = matrix.
    Width; int height = matrix.
    Height; Bitmap bmap = new BitmaP (width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int x = 0; x < width and x + +) {for (int y = 0; y < height; y++) {Bmap. SetPixel (x, Y, matrix.get_renamed (x, y)!=-1?
Colortranslator.fromhtml ("0xff000000"): colortranslator.fromhtml ("0xFFFFFFFF"));
} return BMAP;

 }

PS: Here again for you to recommend a very powerful two-dimensional code online generation tool, free for everyone to use:

Online generation of two-dimensional code tool (enhanced version):
Http://tools.jb51.net/transcoding/jb51qrcode

More interested readers of asp.net related content can view the site topics: "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", "ASP.net Ajax Skills Summary topics" and " Summary of ASP.net caching operation techniques.

I hope this article will help you to ASP.net program design.

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.