C # uses zxing.net to operate QR codes and barcodes

Source: Internet
Author: User

: http://zxingnet.codeplex.com/

Zxing.net is a tool for barcode and QR code under the. NET platform, which is very convenient to use.

First download the binary DLL file, introduce the project;

Code:

C # code
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.ComponentModel;
  4. Using System.Data;
  5. Using System.Drawing;
  6. Using System.Text;
  7. Using System.Windows.Forms;
  8. Using Zxing.qrcode;
  9. Using ZXing;
  10. Using Zxing.common;
  11. Using Zxing.rendering;
  12. Namespace Zxingtest
  13. {
  14. Public partial class Form1:form
  15. {
  16. Encodingoptions options = null;
  17. Barcodewriter writer = null;
  18. Public Form1 ()
  19. {
  20. InitializeComponent ();
  21. options = New Qrcodeencodingoptions
  22. {
  23. Disableeci = true,
  24. CharacterSet = "UTF-8",
  25. Width = Pictureboxqr.width,
  26. Height = Pictureboxqr.height
  27. };
  28. writer = new Barcodewriter ();
  29. Writer. Format = Barcodeformat.qr_code;
  30. Writer. options = options;
  31. }
  32. private void Buttonqr_click (object sender, EventArgs e)
  33. {
  34. if (Textboxtext.text = = string. Empty)
  35. {
  36. MessageBox.Show ("The input cannot be empty!")  ");
  37. return;
  38. }
  39. Bitmap Bitmap = writer. Write (Textboxtext.text);
  40. Pictureboxqr.image = bitmap;
  41. }
  42. }
  43. }

Effect:



Character formatting can be specified when characters are encoded, the default is iso-8859-1 English character set, but generally mobile devices commonly used UTF-8 character set encoding,

Encoding can be set by Qrcodeencodingoptions.

If you want to generate additional zxing-supported barcodes, just modify the Barcodewriter.format.

C # code
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.ComponentModel;
  4. Using System.Data;
  5. Using System.Drawing;
  6. Using System.Text;
  7. Using System.Windows.Forms;
  8. Using Zxing.qrcode;
  9. Using ZXing;
  10. Using Zxing.common;
  11. Using Zxing.rendering;
  12. Namespace Zxingtest
  13. {
  14. Public partial class Form1:form
  15. {
  16. Encodingoptions options = null;
  17. Barcodewriter writer = null;
  18. Public Form1 ()
  19. {
  20. InitializeComponent ();
  21. options = New Encodingoptions
  22. {
  23. //disableeci = True,
  24. //characterset = "UTF-8",
  25. Width = Pictureboxqr.width,
  26. Height = Pictureboxqr.height
  27. };
  28. writer = new Barcodewriter ();
  29. Writer. Format = BARCODEFORMAT.ITF;
  30. Writer. options = options;
  31. }
  32. private void Buttonqr_click (object sender, EventArgs e)
  33. {
  34. if (Textboxtext.text = = string. Empty)
  35. {
  36. MessageBox.Show ("The input cannot be empty!")  ");
  37. return;
  38. }
  39. Bitmap Bitmap = writer. Write (Textboxtext.text);
  40. Pictureboxqr.image = bitmap;
  41. }
  42. }
  43. }

Effect:

The input string needs to conform to the encoded format, or it will error. Decoding method: C # code
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.ComponentModel;
  4. Using System.Data;
  5. Using System.Drawing;
  6. Using System.Text;
  7. Using System.Windows.Forms;
  8. Using Zxing.qrcode;
  9. Using ZXing;
  10. Using Zxing.common;
  11. Using Zxing.rendering;
  12. Namespace Zxingtest
  13. {
  14. Public partial class Form1:form
  15. {
  16. Barcodereader reader = null;
  17. Public Form1 ()
  18. {
  19. InitializeComponent ();
  20. reader = new Barcodereader ();
  21. }
  22. private void Form1_dragenter (object sender, DragEventArgs e)//When drag and drop into form
  23. {
  24. if (e.data.getdatapresent (dataformats.filedrop))
  25. E.effect = dragdropeffects.copy; //Show copy effect
  26. Else
  27. E.effect = DragDropEffects.None;
  28. }
  29. private void Form1_dragdrop (object sender, DragEventArgs e) //When dragged and placed on a form
  30. {
  31. string[] FileNames = (string[]) e.data.getdata (DataFormats.FileDrop, false); //Get file name
  32. if (filenames.length > 0)
  33. {
  34. Pictureboxpic.load (Filenames[0]); //Show Pictures
  35. Result result = reader. Decode ((Bitmap) pictureboxpic.image); //decoding via reader
  36. Textboxtext.text = result. Text; //Show analytic results
  37. }
  38. }
  39. }
  40. }


    • Zxingdecoder.zip (401.4 KB)
    • Download number of times: 117
    • Zxingencoder.zip (395.8 KB)
    • Download number of times: 106
    • View Picture Attachments

C # uses zxing.net to operate QR codes and barcodes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.