分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

Asp.Net生成二维码

发布时间:2023-09-06 01:19责任编辑:赖小花关键词:二维码
一、
 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 ????????????????????????#endregion27 ????????????????????}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 image106 ????????????/// </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生成二维码

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved