ASP.NET 產生二維碼(採用ThoughtWorks.QRCode和QrCode.Net兩種方式)

來源:互聯網
上載者:User

標籤:.net   c#   二維碼   

       最近做項目遇到產生二維碼的問題,發現網上用的最多的是ThoughtWorks.QRCode和QrCode.Net兩種方式。訪問官網看著例子寫了兩個Demo,使用過程中發現兩個都挺好用的,ThoughtWorks.QRCode的功能更多一些,但是dll檔案有6兆,QrCode.Net只有400多K,大家根據自己的需要選擇吧。附上代碼僅供參考。

      並且提供VS2013寫的一個Demo提供給大家免費下載。如有疑問歡迎交流。


ThoughtWorks.QRCode:

private voidCreateQrcode(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 產生二維碼(採用ThoughtWorks.QRCode和QrCode.Net兩種方式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.