ASP. NET generates a QR code (using ThoughtWorks. QRCode and QrCode. Net ),

Source: Internet
Author: User

ASP. NET generates a QR code (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:

Click to download

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.