This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21794|Reply: 1

[WPF] C# WPF uses ZXing to generate QR codes Introduction to ImageSource

[Copy link]
Posted on 12/14/2015 2:39:24 PM | | | |
Introduction: If you need to use the following QR code image generation function in the WPF form program, you can implement it through the method in this article.
Steps: 1. Download ZXing on the http://zxingnet.codeplex.com/ site. .Net
2. After downloading, you can see that there are different targets. .NET version of the dll file that references the correct dll in your project
3. Then refer to the System.Drawing assembly in your project
4. In the window where you need to generate a QR code, add the following code:

  1. // 注销对象方法API

  2. [DllImport("gdi32")]

  3. static extern int DeleteObject(IntPtr o);



  4. /**

  5. * 创建二维码图片

  6. */

  7. private ImageSource createQRCode(String content, int width, int height)

  8. {

  9.     EncodingOptions options;//包含一些编码、大小等的设置

  10.     BarcodeWriter write = null;//用来生成二维码,对应的BarcodeReader用来解码

  11.     options = new QrCodeEncodingOptions

  12.     {

  13.         DisableECI = true,

  14.         CharacterSet = "UTF-8",

  15.         Width = width,

  16.         Height = height,

  17.         Margin = 0

  18.     };

  19.     write = new BarcodeWriter();

  20.     write.Format = BarcodeFormat.QR_CODE;

  21.     write.Options = options;

  22.     Bitmap bitmap = write.Write(content);

  23.     IntPtr ip = bitmap.GetHbitmap();

  24.     BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(

  25.         ip, IntPtr.Zero, Int32Rect.Empty,

  26.         System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

  27.     DeleteObject(ip);

  28.     return bitmapSource;

  29. }
Copy code
5. Call createQRCode to complete the ImageSource generation of the QR code, and then use Image to display it.





Previous:.net list and array
Next:C# specifies the IP segment to generate the IP address
Posted on 12/14/2015 3:55:03 PM |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com