C#利用QrCode.Net產生二維碼(Qr碼)

來源:互聯網
上載者:User

標籤:des   winform   style   blog   http   io   color   os   ar   

在網上很多應用都是用二維碼來分享網址或者其它的資訊。尤其在移動領域,二維碼更是有很大的應用情境。因為項目的需要,需要在網站中增加一個產生二維碼分析網址的功能,在Google大幅度抽筋的情況下無奈使用百度。百度N多,找到一些項目,但是可用性不強。(有一個項目是用VS2005開發的,在2010中調試不開。)終於在codeplex上找到一個“神器”,這個“神器”可以很方便的產生二維碼,速度那是相當的快,並且可支援中文,遵從MIT協議。

QrCode.Net是一個使用C#編寫的用於產生二維碼圖片的類庫,使用它可以非常方便的為WinForm、WebForm、WPF、Silverlight和Windows Phone 7應用程式提供二維碼編碼輸出功能。可以將二維碼檔案匯出為eps格式。

項目地址為:http://qrcodenet.codeplex.com

QrCode.Net不再採用http://code.google.com/p/zxing/ ZXing的連接埠,新的版本將有更好的效能。

測試結果如下(微秒):

輸入字串長度:74個

EC performance 1000 Tests~ QrCode.Net: 3929 ZXing: 5221

同時,QrCode.Net可以對字串進行分析,決定是否使用UTF-8編碼。(比如使用中文的時候。)

1.Graphics

        //const string str = "Hello World!";        const string str = "http://www.weilanwu.com";        protected override void OnPaint(PaintEventArgs e)        {            base.OnPaint(e);            ShowCode(e.Graphics);        }        //在Graphics上繪製二維碼        private void ShowCode(Graphics g)        {            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);            QrCode qrCode = qrEncoder.Encode(str);            FixedModuleSize moduleSize = new FixedModuleSize(2, QuietZoneModules.Two);            GraphicsRenderer render = new GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White);            render.Draw(g, qrCode.Matrix);        }

顯示結果:

2.產生到圖片

        /*         * FixedModuleSize---5-----165*165         * FixedModuleSize---4-----132*132         * FixedModuleSize---3-----99*99         * FixedModuleSize---2-----66*66         * FixedModuleSize---1-----33*33         */        //直接把二維碼 儲存到檔案        private void SaveCodeFile()        {            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);            QrCode code = new QrCode();            qrEncoder.TryEncode(str, out code);            const int modelSizeInPixels = 4;            GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(modelSizeInPixels, QuietZoneModules.Two)                , Brushes.Black, Brushes.White);            string filename = LocalPathHelper.DesktopPath + "/one.png";            using (FileStream stream = new FileStream(filename, FileMode.Create))            {                render.WriteToStream(code.Matrix, ImageFormat.Png, stream);            }        }

顯示結果:

 

C#利用QrCode.Net產生二維碼(Qr碼)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.