Introducción: Si necesitas usar la siguiente función de generación de imágenes por código QR en el programa de formularios WPF, puedes implementarla mediante el método mencionado en este artículo.
Pasos: 1. Descargar ZXing en la web de http://zxingnet.codeplex.com/. .Net 2. Después de descargar, puedes ver que hay diferentes objetivos. .NET del archivo dll que hace referencia al dll correcto en tu proyecto 3. Luego consulta el sistema y el ensamblaje de dibujo en tu proyecto 4. En la ventana donde necesitas generar un código QR, añade el siguiente código:
- // 注销对象方法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;
- }
Copiar código 5. Llama a createQRCode para completar la generación del código QR por ImageSource y luego usa Image para mostrarlo.
|