标签:
有三种方式:
实例源码:
protected void Page_Load(object sender, EventArgs e) { DateTime dtBegin = DateTime.Now; string fileName = AppDomain.CurrentDomain.BaseDirectory + "bin\\" + Guid.NewGuid().ToString() + ".jpg"; string content = HttpUtility.UrlDecode(this.Request["content"]); BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.QR_CODE, Options = new EncodingOptions() { Width = 300, Height = 400 }, Renderer = (IBarcodeRenderer<Bitmap>)Activator.CreateInstance(typeof(BitmapRenderer)) }; Bitmap bmp = writer.Write(content); bmp.Save(fileName); this.Response.WriteFile(fileName); string time = (DateTime.Now - dtBegin).TotalMilliseconds.ToString(); }
实例源码:
protected void Page_Load(object sender, EventArgs e) { DateTime dtBegin = DateTime.Now; string fileName = AppDomain.CurrentDomain.BaseDirectory + "bin\\" + Guid.NewGuid().ToString() + ".jpg"; string content = HttpUtility.UrlDecode(Request.QueryString["content"]); QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = qrEncoder.Encode(content); GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); using (FileStream stream = new FileStream(fileName, FileMode.Create)) { renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream); } this.Response.WriteFile(fileName); string spendTime = (DateTime.Now - dtBegin).TotalMilliseconds.ToString(); }
实例源码:
protected void Page_Load(object sender, EventArgs e) { DateTime dtBegin = DateTime.Now; string content = HttpUtility.UrlDecode(Request.QueryString["content"]); Bitmap bitmap = CreateQRCode(content); string fileName = AppDomain.CurrentDomain.BaseDirectory + "bin\\" + Guid.NewGuid().ToString() + ".jpg"; bitmap.Save(fileName, ImageFormat.Jpeg); Response.WriteFile(fileName,true); string spendTime = (DateTime.Now - dtBegin).TotalMilliseconds.ToString(); } public Bitmap CreateQRCode(string content) { QRCodeEncoder qrEncoder = new QRCodeEncoder(); qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrEncoder.QRCodeScale = 4; //Convert.ToInt32(txtSize.Text); qrEncoder.QRCodeVersion = 7;// Convert.ToInt32(cboVersion.SelectedValue); qrEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; try { Bitmap qrcode = qrEncoder.Encode(content, Encoding.UTF8); return qrcode; } catch (IndexOutOfRangeException ex) { //超出当前二维码版本的容量上限,请选择更高的二维码版本 return new Bitmap(100, 100); } catch (Exception ex) { //生成二维码出错 return new Bitmap(100, 100); } }
注意事项:
1、web端请求二维码的一种方式是将生成二维码的服务页面赋给<img>的src属性。
2、三种方式均为开源项目,使用时到官网下载源码,并添加到项目中即可
3、三种方式对比:3的处理速度最快,在web端访问的处理时间大概20ms,1和2的速度达不多,为50ms到70ms左右,但1和2dll较小,几百k,而3为6M
4、方式3的demo运行时有问题,原因待查
标签:
原文地址:http://www.cnblogs.com/MattCheng/p/4613520.html