标签:
1. 指定某张Logo图片进行生成
须引用ThoughtWorks.QRCode.dll
QrcodeWeChart qwc = new QrcodeWeChart(); // int memberid = 1000000; // int order = 1; // string tkt = qwc.ticket(memberid, order); // Ticket t = JsonConvert.DeserializeObject<Ticket>(tkt); string url = XXXXXXXXXXXXXXXXXXXXXX;//t.url ; //要生成二维码的url地址 //验证网址 if (url.Length == 0) { //Utils.ShowMsg2(this, "请输入网址"); return; } //生成二维码 QRCodeEncoder qrEntity = new QRCodeEncoder(); qrEntity.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//二维码编码方式 qrEntity.QRCodeScale = 10;//每个小方格的宽度 qrEntity.QRCodeVersion = 5;//二维码版本号 qrEntity.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//纠错码等级 System.Drawing.Bitmap srcimage; //动态调整二维码版本号,上限40,过长返回空白图片,编码后字符最大字节长度2953 while (true) { try { srcimage = qrEntity.Encode(url, System.Text.Encoding.UTF8); break; } catch (IndexOutOfRangeException ex) { if (qrEntity.QRCodeVersion < 40) { qrEntity.QRCodeVersion++; } else { srcimage = new Bitmap(300, 300); break; } } } System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/favicon.jpg")); if (img !=null) { //System.IO.Stream picstream = img1.InputStream; //System.Drawing.Image img = System.Drawing.Image.FromStream(picstream); if (img.Width > 250 || img.Height > 250) { img.Dispose(); //(this, "上传Logo图片错误,Logo图片宽度不能超过250px,且高度不能超过250px!"); return; img.Dispose(); } //将logo绘制到二维码 Bitmap bLogo = img as Bitmap; //获取logo图片对象 bLogo = new Bitmap(bLogo, 50, 50); //改变图片的大小这里我们设置为30 int Y = srcimage.Height; int X = srcimage.Width; Point point = new Point(X / 2 - 15, Y / 2 - 15);//logo图片绘制到二维码上,这里将简单计算一下logo所在的坐标 Graphics g = Graphics.FromImage(srcimage);//创建一个画布 g.DrawImage(bLogo, point);//将logo图片绘制到二维码图片上 //imgQR.ImageUrl = srcimage; } string tempDire = Server.MapPath("~/imges/Qt"); if (!Directory.Exists(tempDire)) { Directory.CreateDirectory(tempDire); } string imgPath = "~/imges/Qt/" + DateTime.Now.ToString("yyyy_MM_dd_hh_ss_mm") + ".bmp"; srcimage.Save(Server.MapPath(imgPath));
2. 使用FileupLoad上传的型式上传 须引用ThoughtWorks.QRCode.dll
//验证网址 string url = this.txtURL.Text.Trim(); if (url.Length == 0) { Utils.ShowMsg2(this, "请输入网址"); return; } //生成二维码 QRCodeEncoder qrEntity = new QRCodeEncoder(); qrEntity.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//二维码编码方式 qrEntity.QRCodeScale = 10;//每个小方格的宽度 qrEntity.QRCodeVersion = 5;//二维码版本号 qrEntity.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//纠错码等级 System.Drawing.Bitmap srcimage; //动态调整二维码版本号,上限40,过长返回空白图片,编码后字符最大字节长度2953 while (true) { try { srcimage = qrEntity.Encode(url, System.Text.Encoding.UTF8); break; } catch (IndexOutOfRangeException ex) { if (qrEntity.QRCodeVersion < 40) { qrEntity.QRCodeVersion++; } else { srcimage = new Bitmap(300, 300); break; } } } //验证logo HttpPostedFile logo = fileLogo.PostedFile; if (logo.ContentLength > 0) { if (!Utils.IsImage(logo.InputStream)) { Utils.ShowMsg2(this, "图片格式错误"); return; } System.IO.Stream picstream = logo.InputStream; System.Drawing.Image img = System.Drawing.Image.FromStream(picstream); if (img.Width > 250 || img.Height > 250) { img.Dispose(); Utils.ShowMsg2(this, "上传Logo图片错误,Logo图片宽度不能超过250px,且高度不能超过250px!"); return; img.Dispose(); } //将logo绘制到二维码 Bitmap bLogo = img as Bitmap; //获取logo图片对象 bLogo = new Bitmap(bLogo, 50, 50); //改变图片的大小这里我们设置为30 int Y = srcimage.Height; int X = srcimage.Width; Point point = new Point(X / 2 - 15, Y / 2 - 15);//logo图片绘制到二维码上,这里将简单计算一下logo所在的坐标 Graphics g = Graphics.FromImage(srcimage);//创建一个画布 g.DrawImage(bLogo, point);//将logo图片绘制到二维码图片上 //imgQR.ImageUrl = srcimage; } string tempDire = Server.MapPath("../Upload/supplier/" + CurrentSupplier.SupplierID + "/QrBitMap/"); if (!Directory.Exists(tempDire)) { Directory.CreateDirectory(tempDire); } string imgPath = "../Upload/supplier/" + CurrentSupplier.SupplierID + "/QrBitMap/" + Guid.NewGuid().ToString() + ".bmp"; srcimage.Save(Server.MapPath(imgPath));
标签:
原文地址:http://www.cnblogs.com/yingger/p/4236654.html