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