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

推荐订阅源

S
Schneier on Security
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
The Hacker News
The Hacker News
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
C
Cisco Blogs
AWS News Blog
AWS News Blog
T
Threatpost
L
LINUX DO - 热门话题
Simon Willison's Weblog
Simon Willison's Weblog
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
The Register - Security
The Register - Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
罗磊的独立博客
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
N
News | PayPal Newsroom
Project Zero
Project Zero
NISL@THU
NISL@THU
博客园_首页
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Full Disclosure
T
Troy Hunt's Blog
Recorded Future
Recorded Future
N
Netflix TechBlog - Medium
P
Privacy International News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
Arctic Wolf
C
Check Point Blog
W
WeLiveSecurity
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes

博客园 - 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;
        }

下面是下载地址:

点击下载