码迷,mamicode.com
首页 > Windows程序 > 详细

C#生成二维码

时间:2017-01-13 10:10:39      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:二维   bin   from   link   bit   ntb   generic   save   try   

引用thoughtworks.qrcode.dll

1、不带图片

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Drawing;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Web;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 using ThoughtWorks.QRCode.Codec;
10 
11 namespace makeQrcode
12 {
13     public partial class WebForm1 : System.Web.UI.Page
14     {
15         protected void Page_Load(object sender, EventArgs e)
16         {
17 
18         }
19 
20         protected void createQrCode_Click(object sender, EventArgs e)
21         {
22             createQrCode(this.codeInfo.Text);
23         }
24 
25         private void createQrCode(string text)
26         {
27             Bitmap bt;
28             string enCodeString = text;
29             QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
30             //bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
31             bt = qrCodeEncoder.Encode(enCodeString);
32             string filename = string.Format(DateTime.Now.ToString(), "yyyymmddhhmmss");
33             filename = filename.Replace(" ", "");
34             filename = filename.Replace(":", "");
35             filename = filename.Replace("-", "");
36             filename = filename.Replace(".", "");
37             bt.Save(Server.MapPath("~/image/") + filename + ".jpg");
38             this.qrCodeImage.ImageUrl = "~/image/" + filename + ".jpg";
39         }
40 
41     }
42 }

2、带图片

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Drawing;
 4 using System.Drawing.Drawing2D;
 5 using System.Drawing.Imaging;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Web;
 9 using ThoughtWorks.QRCode.Codec;
10 
11 namespace QRCode.QRCode
12 {
13     /// <summary>
14     /// qrcodeImg 的摘要说明
15     /// </summary>
16     public class qrcodeImg : IHttpHandler
17     {
18         public bool IsReusable
19         {
20             get
21             {
22                 return false;
23             }
24         }
25 
26         public void ProcessRequest(HttpContext context)
27         {
28             //context.Response.ContentType = "text/plain";
29             //context.Response.Write("Hello World");
30             string text = context.Request["CodeText"];
31             string text2 = context.Request["img"];
32             if (text2 == "" || text2 == null)
33             {
34                 text2 = "../images/wm.png";
35             }
36             if (!string.IsNullOrEmpty(text))
37             {
38                 QRCodeEncoder qRCodeEncoder = new QRCodeEncoder();
39                 //qRCodeEncoder.set_QRCodeEncodeMode(2);
40                 //qRCodeEncoder.set_QRCodeScale(4);
41                 //qRCodeEncoder.set_QRCodeVersion(8);
42                 //qRCodeEncoder.set_QRCodeErrorCorrect(1);
43                 qRCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
44                 qRCodeEncoder.QRCodeScale = 4;
45                 qRCodeEncoder.QRCodeVersion = 8;
46                 qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
47                 System.Drawing.Image image = qRCodeEncoder.Encode(text);
48                 MemoryStream memoryStream = new MemoryStream();
49                 image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
50                 MemoryStream memoryStream2 = new MemoryStream();
51                 CombinImage(image, context.Server.MapPath(text2)).Save(memoryStream2, System.Drawing.Imaging.ImageFormat.Png);
52                 context.Response.ClearContent();
53                 context.Response.ContentType = "image/png";
54                 context.Response.BinaryWrite(memoryStream2.ToArray());
55                 memoryStream.Dispose();
56                 memoryStream2.Dispose();
57             }
58             context.Response.Flush();
59             context.Response.End();
60         }
61         public static System.Drawing.Image CombinImage(System.Drawing.Image imgBack, string destImg)
62         {
63             System.Drawing.Image image = System.Drawing.Image.FromFile(destImg);
64             if (image.Height != 65 || image.Width != 65)
65             {
66                 image = KiResizeImage(image, 65, 65, 0);
67             }
68             System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(imgBack);
69             graphics.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);
70             graphics.DrawImage(image, imgBack.Width / 2 - image.Width / 2, imgBack.Width / 2 - image.Width / 2, image.Width, image.Height);
71             GC.Collect();
72             return imgBack;
73         }
74         public static System.Drawing.Image KiResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode)
75         {
76             System.Drawing.Image result;
77             try
78             {
79                 System.Drawing.Image image = new System.Drawing.Bitmap(newW, newH);
80                 System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image);
81                 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
82                 graphics.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, newW, newH), new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.GraphicsUnit.Pixel);
83                 graphics.Dispose();
84                 result = image;
85             }
86             catch
87             {
88                 result = null;
89             }
90             return result;
91         }
92 
93     }
94 }

前端调用(一个简单的下载页面):

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
 6     <title>下载</title>
 7     <style type="text/css">
 8         body
 9         {
10             margin: 0;
11             padding: 0;
12             color: #333333;
13             font-family: "微软雅黑";
14             background: #f0f0f0;
15         }
16         .clear
17         {
18             clear: both;
19         }                     
20         .layout
21         {
22             width: 100%;
23             background: #3d3f40;
24             border-bottom: 1px solid #cccccc;
25         }
26         .layout .logo
27         {
28             max-width: 960px;
29             height: 86px;
30             margin: 0 auto;
31             background: url(../images/plat_logo.png) 10px center no-repeat;
32         }
33         .center
34         {
35             background: #ffffff;
36             border-top: 1px solid #222222;
37         }
38         .center .con
39         {
40             max-width: 960px;
41             margin: 0 auto;
42             padding: 40px 0;
43         }
44         .btn
45         {
46             width: 30%;
47             margin-left: 10px;
48             min-width: 296px;
49             float: left;
50             padding: 5px 5px;
51             text-align: center;
52         }       
53         .btn a.ios
54         {
55             height: 200px;
56             display: block;
57             background: #4691dc url(../images/ios.png) center center no-repeat;
58             margin: 0 auto;
59         }
60         .btn a.android
61         {
62             height: 200px;
63             display: block;
64             background: #a4c63e url(../images/android.png) center center no-repeat;
65             margin: 0 auto;
66         }             
67     </style>
68 </head>
69 <body>
70     <div class="layout">
71         <div class="logo">
72         </div>
73         <div class="center">
74             <div class="con">                        
75                 <div class="btn">
76                     <a id="iphoneLink" href="#" class="ios"></a>
77                 </div>
78                 <div class="btn">
79                     <a href="#" class="android"></a>
80                 </div>
81                 <div class="btn">
82                     <img id="QrCodeImg" alt="" src="" />
83                 </div>
84                 <div class="clear">
85                 </div>
86             </div>
87         </div>
88     </div>    
89     <script type="text/javascript">      
90 
91         var hostname = location.hostname;
92         var port = location.port;
93         var page = "http://" + hostname + ":" + port + "/QRCode/install.html";
94        
95         document.getElementById("QrCodeImg").src = "qrcodeImg.ashx?CodeText=" + page;        
96     </script>
97 </body>
98 </html>

 

C#生成二维码

标签:二维   bin   from   link   bit   ntb   generic   save   try   

原文地址:http://www.cnblogs.com/wuln/p/6281479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!