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.