Introduction : Si vous devez utiliser la fonction suivante de génération d’images en QR code dans le programme de formulaires WPF, vous pouvez l’implémenter via la méthode de cet article.
Étapes : 1. Télécharger ZXing sur le site http://zxingnet.codeplex.com/. .Net 2. Après téléchargement, vous pouvez voir qu’il y a différentes cibles. .NET du fichier dll qui fait référence à la bonne dll dans votre projet 3. Alors référez-vous au système. Assemblage de dessins dans votre projet 4. Dans la fenêtre où vous devez générer un code QR, ajoutez le code suivant :
- // 注销对象方法API
- [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;
- }
Code de copie 5. Appeler createQRCode pour compléter la génération du code QR par ImageSource, puis utiliser Image pour l’afficher.
|