日期:2014-05-16 浏览次数:20973 次
自己参考QrCode.Net开源实例封装了一个生成二维码的静态方法如下:
/// <summary> /// 含有QR码的描述类和包装编码和渲染 /// </summary> public class QRCodeHelper { /// <summary> /// 获取二维码 /// </summary> /// <param name="strContent">待编码的字符</param> /// <param name="ms">输出流</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; //误差校正水平 string Content = strContent;//待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12;//大小 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); } else { return false; } return true; } }
vs2010 framework3.5 引用的组件是QrCode.Net 0.4 Pre-Release\Gma.QrCodeNet.Encoding.Net35
public ActionResult Index() { // Render the QR code as an image using (var ms = new MemoryStream()) { string stringtest="中国inghttp://www.baidu.com/mvc.test?&"; QRCodeHelper.GetQRCode(stringtest, ms); Response.ContentType = "image/Png"; Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length); Response.End(); } return View(); }