您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關ASP.NET如何生成二維碼,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
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; }
關于“ASP.NET如何生成二維碼”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。