码迷,mamicode.com
首页 > Web开发 > 详细

Asp.Net生成二维码

时间:2017-10-23 18:18:05      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:content   生成   show   war   directory   err   false   oms   nta   

一、
 1             try
 2             {
 3                 while (this.UploadQueue.Count > 0)
 4                 {
 5                     var row = this.UploadQueue.Dequeue();
 6                     string url = row[2].ToString();
 7                     GC.Collect();
 8                     using (var ms = new MemoryStream())
 9                     {
10                         string strName = row[1].ToString();
11                         strName = System.IO.Path.GetFileNameWithoutExtension(strName.Substring(strName.LastIndexOf(/) + 1));
12 
13                         #region 保存到本地
14                         ThoughtWorks.QRCode.Codec.QRCodeEncoder qrc = new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
15                         qrc.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;
16                         qrc.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;
17                         qrc.QRCodeScale = 8;
18                         qrc.QRCodeVersion = 4;
19 
20                         Bitmap bitmap = qrc.Encode(url);
21                         string path = BaseDirectory + "\\二维码图片\\";
22                         if (Directory.Exists(path) == false) Directory.CreateDirectory(path);
23                         string filename = System.IO.Path.Combine(path, strName + ".png"); ;//保存到本地
24                         bitmap.Save(filename: filename);
25                         bitmap.Dispose();
26                         #endregion
27                     }
28                 }
29             }
30             catch (Exception exception)
31             {
32                 Show(exception.Message);
33             }

 






二、
  1         protected void Button1_Click(object sender, EventArgs e)
  2         {
  3             string filePath = ""; ;
  4             FileStream stream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read);
  5             //1. Reading from a binary Excel file (‘97-2003 format; *.xls)
  6             //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
  7 
  8             //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
  9             //IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
 10             string strExtension = System.IO.Path.GetExtension(filePath);
 11             IExcelDataReader excelReader = null;
 12             //区别excel版本
 13             if (strExtension.IndexOf("xlsx")>=0)
 14             {
 15                 excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
 16             }
 17             else
 18             {
 19                 excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
 20             }
 21             excelReader.IsFirstRowAsColumnNames = false;
 22             DataSet result = excelReader.AsDataSet();
 23             for (int i = 1; i < result.Tables["Sheet1"].Rows.Count; i++)
 24             {
 25                 string strUrl = Convert.ToString(result.Tables["Sheet1"].Rows[i][0]);
 26                 
 27                 string strName = result.Tables["Sheet1"].Rows[i][1].ToString();
 28 
 29                 ///Andeliya/AwardWeb/index?id=1
 30                 string href = strUrl;
 31                 string empleename = strName;//id; //strUrl.Remove(0, 38);
 32                 var codeParams = CodeDescriptor.Init("M", href, "Zero", "12");
 33                 if (codeParams == null || !codeParams.TryEncode())
 34                 {
 35                     Response.StatusCode = (int)HttpStatusCode.BadRequest;
 36                     Response.Write("参数不能为空!");
 37                 }
 38                 // Render the QR code as an image
 39                 System.GC.Collect();
 40                 using (var ms = new MemoryStream())
 41                 {
 42                     codeParams.Render(ms);
 43                     #region 保存到本地
 44                     System.IO.MemoryStream msd = new System.IO.MemoryStream(ms.GetBuffer());
 45                     System.Drawing.Image img = System.Drawing.Image.FromStream(msd);
 46                     var temp = string.Format(Server.MapPath("~/res/2017101811/"));
 47                     if (!Directory.Exists(temp)) { Directory.CreateDirectory(temp); }
 48                     string path = temp + "/" + empleename + ".png";//保存到本地
 49                     img.Save(path);//随机名 
 50                     //return Controller.File(path, "application/msword", empleename + "的二维码" + DateTime.Now.ToString("yyMMddhhmmss") + ".png");//输出到浏览器
 51                     #endregion
 52                 }
 53                 Response.Write(empleename + "二维码生成成功");
 54                 Response.Write("<br/>");
 55             }
 56             Response.Write("二维码生成成功!");
 57         }
 58         /// <summary>
 59         /// 
 60         /// </summary>
 61         internal class CodeDescriptor
 62         {
 63             public ErrorCorrectionLevel Ecl;
 64             public string Content;
 65             public QuietZoneModules QuietZones;
 66             public int ModuleSize;
 67             public BitMatrix Matrix;
 68             public string ContentType;
 69             /// <summary>
 70             /// Parse QueryString that define the QR code properties
 71             /// </summary>
 72             /// <param name="request">HttpRequest containing HTTP GET data</param>
 73             /// <returns>A QR code descriptor object</returns>
 74             public static CodeDescriptor Init(string e, string t, string q, string s)
 75             {
 76                 var cp = new CodeDescriptor();
 77                 // Error correction level
 78                 if (!Enum.TryParse(e, out cp.Ecl))
 79                     cp.Ecl = ErrorCorrectionLevel.L;
 80                 // Code content to encode
 81                 cp.Content = t;
 82                 // Size of the quiet zone
 83                 if (!Enum.TryParse(q, out cp.QuietZones))
 84                     cp.QuietZones = QuietZoneModules.Two;
 85                 // Module size
 86                 if (!int.TryParse(s, out cp.ModuleSize))
 87                     cp.ModuleSize = 12;
 88                 return cp;
 89             }
 90             /// <summary>
 91             /// Encode the content with desired parameters and save the generated Matrix
 92             /// </summary>
 93             /// <returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>
 94             public bool TryEncode()
 95             {
 96                 var encoder = new QrEncoder(Ecl);
 97                 QrCode qr;
 98                 if (!encoder.TryEncode(Content, out qr))
 99                     return false;
100 
101                 Matrix = qr.Matrix;
102                 return true;
103             }
104             /// <summary>
105             /// Render the Matrix as a PNG image
106             /// </summary>
107             /// <param name="ms">MemoryStream to store the image bytes into</param>
108             internal void Render(MemoryStream ms)
109             {
110                 var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
111                 render.WriteToStream(Matrix, ImageFormat.Png, ms);
112                 ContentType = "image/png";
113             }
114         }

 


Asp.Net生成二维码

标签:content   生成   show   war   directory   err   false   oms   nta   

原文地址:http://www.cnblogs.com/shiyige-216/p/7717838.html

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