Click here to Skip to main content
15,901,666 members
Articles / Programming Languages / C#

Basic with QR Code using Zxing Library

Rate me:
Please Sign up or sign in to vote.
4.55/5 (26 votes)
12 Jul 2020CPOL2 min read 220.9K   53   46
Encoded, decoded your QR code using Zxing library
In this tip, I'll cover a simple method to do with a QR code inside a standard control.

Introduction

For reference, I will use ZXing.Net library from zxingnet.codeplex.com.

First, you will need to download the ZXing.Net library from zxingnet.codeplex.com. Extract the contents of the file you have downloaded and reference the library that fits your needs in your project.

Download link: ZXing.Net.0.14.0.0.zip (Released: Apr 7, 2014)

The Zxing.Net project now migrated to https://github.com/micjahn/ZXing.Net (Reference: Feb 3,2018)

First please add this references to your Project References before using Zxing.Net libraries (Reference: Feb 3,2018)

Background

About ZXing.Net
A library which supports decoding and generating of barcodes (like QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within images.

Using the Code

source code ทั้งหมดที่ใช้ในบทความนี้ครับ : )

https://github.com/lazycat18/Workshop-Basic-with-QR-Code-using-Zxing-Library

Design for this tip looks like this... : )

Step 1: Using Zxing Library

using Zxing library from your reference.

C#
using ZXing.Common;
using ZXing;
using ZXing.QrCode;

Step 2: Coding in your Form Load

For Width or Height, you can change it to any value you want. :)

Note: Please write this first:

QrCodeEncodingOptions options = new QrCodeEncodingOptions();

C#
options =  new QrCodeEncodingOptions
{
    DisableECI = true,
    CharacterSet = "UTF-8",
    Width = 250,
    Height = 250,
};
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options; 

//update : Feb 3,2018

Step 3: Create Generate QR Code Button

Generate your text from textBox1 to QR Code format and show this result in pictureBox1:

C#
if (String.IsNullOrWhiteSpace(textBox1.Text)||String.IsNullOrEmpty(textBox1.Text)){
          pictureBox1.Image = null;
          MessageBox.Show("Text not found", "Oops!",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else{
          var qr = new ZXing.BarcodeWriter();
          qr.Options = options;
          qr.Format = ZXing.BarcodeFormat.QR_CODE;
          var result = new Bitmap(qr.Write(textBox1.Text.Trim()));
          pictureBox1.Image = result;
          textBox1.Clear();
}
//update : Feb 3,2018

Step 4: Create Decode QR Code Button

Decoded your QR Code from pictureBox1 to plain text and show this result in textBox1:

C#
try{
    Bitmap bitmap = new Bitmap(pictureBox1.Image);
    BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryInverted = true };
    Result result = reader.Decode(bitmap);
    string decoded = result.ToString().Trim();
    textBox1.Text = decoded;
}catch (Exception){
    MessageBox.Show("Image not found", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//update : Feb 3,2018

Step 5: Create Browse a Local Image Button

Find where image files are stored on your computer.

C#
OpenFileDialog open = new OpenFileDialog();
      if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            var qr = new ZXing.BarcodeWriter();
            qr.Options = options;
            qr.Format = ZXing.BarcodeFormat.QR_CODE;
            pictureBox1.ImageLocation = open.FileName;
}
       
//update : Feb 3,2018

Step 6: Create Download Button

Save your QR code with file format type like this *.png, *. jpg, *.bmp, *.gif.

C#
if(pictureBox1.Image == null)
            {
                MessageBox.Show("Image not found", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                SaveFileDialog save = new SaveFileDialog();
                save.CreatePrompt = true;
                save.OverwritePrompt = true;
                save.FileName = "QR";
                save.Filter = "PNG|*.png|JPEG|*.jpg|BMP|*.bmp|GIF|*.gif";
                if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    pictureBox1.Image.Save(save.FileName);
                    save.InitialDirectory = Environment.GetFolderPath
                                (Environment.SpecialFolder.Desktop);
                }
            }
//update : Feb 3,2018

Points of Interest

  • Encoding text from Thai language and decoding this. Now show the correct result!! You can try this with your QR code reader.

History

  • 29/06/2015: First release for www.codeproject.com
  • 03/02/2018: Second update for several years. This update comes with new Zxing.Net Libraries using NuGet Package (easy to use)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Thailand Thailand
I love advanced technology and like to know a lot about all kinds of tech

Education
Kasetsart University
Bachelor of Science Program in Computer Science | 2011-2015

Comments and Discussions

 
SuggestionRe: is any declration need for 'options' in formload() function ? Pin
Sanpath Sunggad3-Feb-18 7:21
Sanpath Sunggad3-Feb-18 7:21 
AnswerRe: is any declration need for 'options' in formload() function ? Pin
Sanpath Sunggad4-Feb-18 23:14
Sanpath Sunggad4-Feb-18 23:14 
GeneralRe: is any declration need for 'options' in formload() function ? Pin
Member 136537078-Feb-18 18:02
Member 136537078-Feb-18 18:02 
QuestionQR Code Pin
saticin27-Jan-18 1:19
saticin27-Jan-18 1:19 
PraiseRe: QR Code Pin
Sanpath Sunggad3-Feb-18 7:46
Sanpath Sunggad3-Feb-18 7:46 
QuestionReading Barcode from Camera Pin
tarunbhardwaj28-Aug-17 23:25
tarunbhardwaj28-Aug-17 23:25 
SuggestionRe: Reading Barcode from Camera Pin
Sanpath Sunggad3-Feb-18 7:42
Sanpath Sunggad3-Feb-18 7:42 
QuestionIS it possible to create vcard? Pin
jfshimul9-May-17 9:07
professionaljfshimul9-May-17 9:07 
I want to create vcard is it possible using this library?
AnswerRe: IS it possible to create vcard? Pin
Sanpath Sunggad24-May-17 16:21
Sanpath Sunggad24-May-17 16:21 
QuestionGreat job Pin
akulovac27-Apr-17 22:02
akulovac27-Apr-17 22:02 
PraiseRe: Great job Pin
Sanpath Sunggad24-May-17 16:11
Sanpath Sunggad24-May-17 16:11 
PraiseMy Vote of 5 Pin
prckmi6-Apr-17 19:32
prckmi6-Apr-17 19:32 
PraiseRe: My Vote of 5 Pin
Sanpath Sunggad24-May-17 16:08
Sanpath Sunggad24-May-17 16:08 
QuestionQR Code Pin
Member 1048524213-Mar-17 1:30
Member 1048524213-Mar-17 1:30 
AnswerRe: QR Code Pin
Sanpath Sunggad24-May-17 16:27
Sanpath Sunggad24-May-17 16:27 
SuggestionThanks for the code Pin
rugby23127-Nov-16 21:19
rugby23127-Nov-16 21:19 
PraiseRe: Thanks for the code Pin
Sanpath Sunggad24-May-17 16:06
Sanpath Sunggad24-May-17 16:06 
Questionnot able to decode Pin
Member 127705633-Oct-16 5:26
Member 127705633-Oct-16 5:26 
AnswerRe: not able to decode Pin
Sanpath Sunggad24-May-17 16:47
Sanpath Sunggad24-May-17 16:47 
PraiseI voted 5 Pin
Ashraf Sabry4-Feb-16 4:15
Ashraf Sabry4-Feb-16 4:15 
PraiseRe: I voted 5 Pin
Sanpath Sunggad24-May-17 16:02
Sanpath Sunggad24-May-17 16:02 
Questionnot able to generate QR code with Character Pin
SathishMask7-Sep-15 20:49
SathishMask7-Sep-15 20:49 
SuggestionRe: not able to generate QR code with Character Pin
Sanpath Sunggad5-Oct-15 1:34
Sanpath Sunggad5-Oct-15 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.