C# WPF使用ZXing產生二維碼ImageSource

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   使用   for   sp   

1、在http://zxingnet.codeplex.com/網站上下載ZXing .Net的第三方庫

2、下載後解壓可以看到有針對不同.Net版本的dll檔案,在你的工程中引用正確的dll

3、然後再你的工程中引用System.Drawing程式集

4、在你需要產生二維碼的Window中,加入一下代碼

// 回收對象[DllImport("gdi32")]static extern int DeleteObject(IntPtr o);
        /**         * 建立二維碼圖片         */        private ImageSource createQRCode(String content, int width, int height)        {            EncodingOptions options;//包含一些編碼、大小等的設定            BarcodeWriter write = null;//用來產生二維碼,對應的BarcodeReader用來解碼            options = new QrCodeEncodingOptions            {                DisableECI = true,                CharacterSet = "UTF-8",                Width = width,                Height = height,                Margin = 0            };            write = new BarcodeWriter();            write.Format = BarcodeFormat.QR_CODE;            write.Options = options;            Bitmap bitmap = write.Write(content);            IntPtr ip = bitmap.GetHbitmap();            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(                ip, IntPtr.Zero, Int32Rect.Empty,                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());            DeleteObject(ip);            return bitmapSource;        }

5、調用createQRCode即可完成二維碼的ImageSource產生,然後使用Image即可顯示

 

C# WPF使用ZXing產生二維碼ImageSource

聯繫我們

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