Recently doing projects encountered the problem of generating two-dimensional code, found that the most used online is thoughtworks.qrcode and qrcode.net two ways. Visit the official website to see Example wrote two demo, using the process found that two are pretty good, thoughtworks.qrcode function more, but the DLL file has 6 trillion, qrcode.net only more than 400 K, we need to choose according to their own. The attached code is for reference only.
Thoughtworks.qrcode:
| The code is as follows |
Copy Code |
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:
| The code is as follows |
Copy Code |
protected void Button1_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>
Get two dimensional code
</summary>
<param name= "Strcontent" > characters to be encoded </param>
<param name= "MS" > Output Flow </param>
<returns>true if the encoding succeeded, false if the content is empty or too large to fit in a QR Code</retur Ns>
public static bool Getqrcode (string strcontent, MemoryStream ms)
{
Errorcorrectionlevel Ecl = errorcorrectionlevel.m; Error correction level
String content = strcontent;//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 contents 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;
} |