Describes how to generate a QR code instance using ASP. NET (using ThoughtWorks. QRCode and QrCode. Net ),

Source: Internet
Author: User

Describes how to generate a QR code instance using ASP. NET (using ThoughtWorks. QRCode and QrCode. Net ),

Recently, I encountered the problem of generating QR codes in my project. I found that ThoughtWorks. QRCode and QrCode. Net are the most commonly used methods on the Internet. Visit the official website and look at the example to write two demos. during use, we found that both of them are quite useful, ThoughtWorks. QRCode has more functions, but the dll file has 6 MB and QrCode. net only has more than 400 K. You can choose according to your own needs. The attached code is for reference only.

A Demo written in VS2013 is provided for free download. If you have any questions, please contact us.

ThoughtWorks. QRCode:

private void CreateQrcode(string nr)    {      Bitmap bt;      string enCodeString = nr;      QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();      bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);      string filename = DateTime.Now.ToString("yyyymmddhhmmss");      string path = Server.MapPath("~/image/") + filename + ".jpg";      Response.Write(path);      bt.Save(path);      this.Image1.ImageUrl = "~/image/" + filename + ".jpg";    }

QrCode. Net:

Protected void button#click (object sender, EventArgs e) {using (var MS = new MemoryStream () {string stringtest = "China inghttp: // www.baidu.com/mvc.test? & "; GetQRCode (stringtest, MS); Response. contentType = "image/Png"; Response. outputStream. write (ms. getBuffer (), 0, (int) ms. length); Image img = Image. fromStream (MS); string filename = DateTime. now. toString ("yyyymmddhhmmss"); string path = Server. mapPath ("~ /Image/") + filename +". png "; img. Save (path); Response. End ();}}
/// <Summary> /// obtain the QR code /// </summary> /// <param name = "strContent"> characters to be encoded </param> /// <param name = "ms"> output stream </param> // <returns> True if the encoding succeeded, false if the content is empty or too large to fit in a QR code </returns> public static bool GetQRCode (string strContent, MemoryStream MS) {ErrorCorrectionLevel Ecl = ErrorCorrectionLevel. m; // Error Correction level string Content = strContent; // the Content to be encoded, QuietZoneModules QuietZones = QuietZoneModules. two; // blank area int ModuleSize = 12; // size var encoder = new QrEncoder (Ecl); QrCode qr; if (encoder. tryEncode (Content, out qr) // encode the Content and save the Generated Matrix {var render = new GraphicsRenderer (new FixedModuleSize (ModuleSize, QuietZones); render. writeToStream (qr. matrix, ImageFormat. png, MS);} else {return false;} return true ;}

Below is: demo

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.