设为首页 加入收藏

TOP

详细QRCode生成二维码和下载实现案例(一)
2019-09-17 19:07:39 】 浏览:50
Tags:详细 QRCode 生成 二维 下载 实现 案例
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using ThoughtWorks.QRCode.Codec;
  6 using System.Drawing;
  7 using System.Drawing.Imaging;
  8 using Game.Utils;
  9 using System.Drawing.Drawing2D;
 10 using Game.Facade;
 11 using System.Net;
 12 using System.IO;
 13 
 14 namespace Game.Web.WS
 15 {
 16     /// <summary>
 17     /// QRCode 的摘要说明
 18     /// </summary>
 19     public class QRCode : IHttpHandler
 20     {
 21 
 22         public void ProcessRequest(HttpContext context)
 23         {
 24             GetQRCode(context);
 25         }
 26 
 27         /// <summary>
 28         /// 绘制二维码
 29         /// </summary>
 30         /// <param name="context"></param>
 31         private void GetQRCode(HttpContext context)
 32         {
 33             string encodeData = GameRequest.GetQueryString("qt");
 34             string icoURL = GameRequest.GetQueryString("qm");
 35             int width = GameRequest.GetQueryInt("qs", 0);
 36             if (encodeData != string.Empty)
 37             {
 38                 calQrcode(encodeData, icoURL, width, context);
 39             }
 40         }
 41 
 42         /// <summary>
 43         /// 按照指定的大小绘制二维码
 44         /// </summary>
 45         /// <param name="sData"></param>
 46         /// <param name="width"></param>
 47         /// <returns></returns>
 48         private void calQrcode(string sData, string icoURL, int size, HttpContext context)
 49         {
 50             //二维码版本,大小获取
 51             Color qrCodeBackgroundColor = Color.White;
 52             Color qrCodeForegroundColor = Color.Black;
 53             int length = System.Text.Encoding.UTF8.GetBytes(sData).Length;
 54 
 55             //生成二维码数据
 56             QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
 57             qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
 58             qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//使用M纠错级别
 59             qrCodeEncoder.QRCodeVersion = 0;
 60             var encodedData = qrCodeEncoder.Encode(sData, System.Text.Encoding.UTF8);
 61 
 62             //绘制图片
 63             int x = 0, y = 0;
 64             int w = 0, h = 0;
 65             // 二维码矩阵单边数据点数目
 66             int count = encodedData.Length;
 67             // 获取单个数据点边长
 68             double sideLength = Convert.ToDouble(size) / count;
 69             // 初始化背景色画笔
 70             SolidBrush backcolor = new SolidBrush(qrCodeBackgroundColor);
 71             // 初始化前景色画笔
 72             SolidBrush forecolor = new SolidBrush(qrCodeForegroundColor);
 73             // 定义画布
 74             Bitmap image = new Bitmap(size, size);
 75             // 获取GDI+绘图图画
 76             Graphics graph = Graphics.FromImage(image);
 77             // 先填充背景色
 78             graph.FillRectangle(backcolor, 0, 0, size, size);
 79 
 80             // 变量数据矩阵生成二维码
 81             for (int row = 0; row < count; row++)
 82             {
 83                 for (int col = 0; col < count; col++)
 84                 {
 85                     // 计算数据点矩阵起始坐标和宽高
 86                     x = Convert.ToInt32(Math.Round(col * sideLength));
 87                     y = Convert.ToInt32(Math.Round(row * sideLength));
 88                     w = Convert.ToInt32(Math.Ceiling((col + 1) * sideLength) - Math.Floor(col * sideLength));
 89                     h = Convert.ToInt32(Math.Ceiling((row + 1) * sideLength) - Math.Floor(row * sideLength));
 90 
 91                     // 绘制数据矩阵
 92                     graph.FillRectangle(encodedData[col][row] ? forecolor : backcolor, x, y, w, h);
 93                 }
 94             }
 95 
 96             //添加LOGO
 97             string path = context.Server.MapPath("/favicon.ico");            
 98             Bitmap logoImage = null;
 99             FileInfo fileInfo = new FileInfo(path);
100             if (fileInfo.Exists)
101             {
102                 logoImage = new Bitmap(path);
103             }
104             if (icoURL != "")
105             {
106                 HttpWebRequest webRequest = (HttpWebRequest)HttpWeb
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SSRS分页 下一篇C# -Asp.Net.SignalR.Core之Hub

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目