Click here to Skip to main content
16,018,318 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 228.3K   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

 
SuggestionUpdate the code to work in .Net 5 or higher Pin
Mahfoud Bouabdallah 20223-Mar-23 8:28
Mahfoud Bouabdallah 20223-Mar-23 8:28 
QuestionUsing Zxing library with web form Pin
nawras al abbas28-Jul-22 19:17
nawras al abbas28-Jul-22 19:17 
GeneralRe: Using Zxing library with web form Pin
Sanpath Sunggad27-Aug-22 18:49
Sanpath Sunggad27-Aug-22 18:49 
QuestionCrystal reports Pin
Member 1199917326-Oct-21 2:52
Member 1199917326-Oct-21 2:52 
QuestionConfigs needed Pin
Élio Mota13-Nov-20 7:27
Élio Mota13-Nov-20 7:27 
GeneralMy vote of 5 Pin
Member 1370414313-Jul-20 2:22
Member 1370414313-Jul-20 2:22 
PraiseRe: My vote of 5 Pin
Sanpath Sunggad14-Jul-20 4:06
Sanpath Sunggad14-Jul-20 4:06 
Questionสวัสดีครับ ผมมือใหม่ เริ่มหัดทำครับ ผมใช้ vs code (C#) แล้วลองทำตามดูครับ แต่มันมี error ที่ code compile ไม่ผ่านครับ Pin
Yongyai1-Jun-20 22:41
Yongyai1-Jun-20 22:41 
AnswerRe: สวัสดีครับ ผมมือใหม่ เริ่มหัดทำครับ ผมใช้ vs code (C#) แล้วลองทำตามดูครับ แต่มันมี error ที่ code compile ไม่ผ่านครับ Pin
Sanpath Sunggad12-Jul-20 3:48
Sanpath Sunggad12-Jul-20 3:48 
QuestionHow do we add new line or line break in our content to be encoded and read back in QR Code Pin
Lakshmi Goyal24-Jul-19 19:25
Lakshmi Goyal24-Jul-19 19:25 
QuestionDYNAMIC QR CODE Pin
Member 1373770119-Jun-18 16:13
Member 1373770119-Jun-18 16:13 
QuestionDoes Zxing library use native code in C or C++? Pin
Member 852948522-Mar-18 3:23
Member 852948522-Mar-18 3:23 
QuestionARABIC QR CODE Pin
Member 1054250021-Mar-18 0:53
Member 1054250021-Mar-18 0:53 
NewsRe: ARABIC QR CODE Pin
Sanpath Sunggad18-Jul-20 18:57
Sanpath Sunggad18-Jul-20 18:57 
QuestionARABIC QR CODE Pin
Member 1054250021-Mar-18 0:50
Member 1054250021-Mar-18 0:50 
QuestionPDF417 Pin
tungtranthanh7-Mar-18 16:10
tungtranthanh7-Mar-18 16:10 
Questionของผมเป็น C# 2.0 ใช้ตัวแปร var ไม่ได้ ต้องทำวิธีไหน แทนครับ Pin
Member 1367926718-Feb-18 21:28
Member 1367926718-Feb-18 21:28 
AnswerRe: ของผมเป็น C# 2.0 ใช้ตัวแปร var ไม่ได้ ต้องทำวิธีไหน แทนครับ Pin
Sanpath Sunggad24-Feb-18 18:20
Sanpath Sunggad24-Feb-18 18:20 
QuestionNice Article, some suggestions... Pin
AngelBlueSky9-Feb-18 8:59
AngelBlueSky9-Feb-18 8:59 
PraiseRe: Nice Article, some suggestions... Pin
Sanpath Sunggad10-Feb-18 4:47
Sanpath Sunggad10-Feb-18 4:47 
GeneralRe: Nice Article, some suggestions... Pin
AngelBlueSky11-Feb-18 23:37
AngelBlueSky11-Feb-18 23:37 
Questionis any declration need for 'options' in formload() function ? Pin
Member 1365370731-Jan-18 1:17
Member 1365370731-Jan-18 1:17 
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 

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.