惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

PCI Perspectives
PCI Perspectives
J
Java Code Geeks
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
博客园 - 聂微东
雷峰网
雷峰网
The Cloudflare Blog
V
V2EX
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News - Newest:
Hacker News - Newest: "LLM"
爱范儿
爱范儿
NISL@THU
NISL@THU
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
腾讯CDC
罗磊的独立博客
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
S
Security Affairs
S
SegmentFault 最新的问题
月光博客
月光博客
P
Privacy International News Feed
Last Week in AI
Last Week in AI
博客园 - Franky
C
Cisco Blogs
宝玉的分享
宝玉的分享
Forbes - Security
Forbes - Security
WordPress大学
WordPress大学
博客园 - 叶小钗
L
LINUX DO - 最新话题
博客园_首页
Spread Privacy
Spread Privacy
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
IT之家
IT之家
人人都是产品经理
人人都是产品经理
I
Intezer
W
WeLiveSecurity
C
CERT Recently Published Vulnerability Notes
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyberwarzone
Cyberwarzone
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Security @ Cisco Blogs
L
Lohrmann on Cybersecurity
美团技术团队

博客园 - Ranran

ASP.NET 5与MVC 6中的新特性 .NET技术大系概览 (迄今为止最全的.NET技术栈) 7 天玩转 ASP.NET MVC — 第 1 天 腾讯web前端笔试题及个人答案 11个Visual Studio代码性能分析工具 高效的使用 Response.Redirect CLR 这些年有啥变化吗? ASP.NET 大文件下载的实现思路及代码 7个Linux和Ubuntu下的免费CSS编辑器 11个很棒的 jQuery 图表库 javascript客户端检测技术 编写更好的C#代码 ASP.NET中gridview获取当前行的索引值 .NET逻辑分层架构总结 ASP.NET MVC 4 的JS/CSS打包压缩功能-------过滤文件 ASP.NET中利用DataList实现图片无缝滚动 老码农教你在 StackOverflow 上谈笑风生 ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式) Asp.net面试题
ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
Ranran · 2015-06-10 · via 博客园 - Ranran

     最近做项目遇到生成二维码的问题,发现网上用的最多的是ThoughtWorks.QRCode和QrCode.Net两种方式。访问官网看着例子写了两个Demo,使用过程中发现两个都挺好用的,ThoughtWorks.QRCode的功能更多一些,但是dll文件有6兆,QrCode.Net只有400多K,大家根据自己的需要选择吧。附上代码仅供参考。

      并且提供VS2013写的一个Demo提供给大家免费下载。如有疑问欢迎交流。

ThoughtWorks.QRCode

private void CreateQrcode(string nr)
        {
            Bitmap bt;
            string enCodeString = nr;
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
            string filename = DateTime.Now.ToString("yyyymmddhhmmss");
            string path = Server.MapPath("~/image/") + filename + ".jpg";
            Response.Write(path);
            bt.Save(path);
            this.Image1.ImageUrl = "~/image/" + filename + ".jpg";
        }

QrCode.Net:

protected void Button1_Click(object sender, EventArgs e)
        {
            using (var ms = new MemoryStream())
            {
                string stringtest = "中国inghttp://www.baidu.com/mvc.test?&";
                GetQRCode(stringtest, ms);
                Response.ContentType = "image/Png";
                Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length);

                Image img = Image.FromStream(ms);
                string filename = DateTime.Now.ToString("yyyymmddhhmmss");
                string path = Server.MapPath("~/image/") + filename + ".png";
                img.Save(path);

                Response.End();
            }  

        }

        /// <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;
        }

下面是下载地址:

点击下载