標籤: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