はじめに:WPFフォームプログラムで以下のQRコード画像生成機能を使う必要がある場合は、この記事の方法で実装できます。
手順:1. http://zxingnet.codeplex.com/ サイトでZXをダウンロード。 .Net 2. ダウンロード後、異なるターゲットがあることがわかります。 プロジェクト内の正しいdllを参照する.NET版のdllファイル 3. 次に、プロジェクト内のSystem.Drawingアセンブリを参照します 4. QRコードを生成するウィンドウに、以下のコードを追加します:
- // 注销对象方法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;
- }
コードをコピーします 5. createQRCodeに連絡してImageSourceのQRコード生成を完了し、その後Imageを使って表示します。
|