소개: WPF 양식 프로그램에서 다음 QR 코드 이미지 생성 기능을 사용해야 한다면, 이 글의 방법을 통해 구현할 수 있습니다.
단계: 1. http://zxingnet.codeplex.com/ 사이트에서 ZXing을 다운로드하세요. .net 2. 다운로드를 마친 후, 다양한 타겟이 있다는 것을 알 수 있습니다. 프로젝트에서 올바른 dll을 참조하는 .NET 버전의 dll 파일 3. 그 다음 프로젝트의 System.Drawing assembly를 참조하세요 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에 호출하여 QR 코드의 ImageSource 생성을 완료한 후 Image를 사용해 QR 코드를 표시합니다.
|