使用ThoughtWorks.QRCode.dll 生成二维码,还需要用到Gma.QrCodeNet.Encoding.Net45.dll 这两个DLL请自行下载,百度一搜一大堆。
/// <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 void GetQRCode2(string strContent, MemoryStream ms) { string Content = strContent;//待编码内容 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方法(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字) qrCodeEncoder.QRCodeScale = 4;//大小 qrCodeEncoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误) qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; System.Drawing.Bitmap bp = qrCodeEncoder.Encode(Content, Encoding.GetEncoding("GB2312")); bp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); } /// <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; }