Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This tutorial will provide an in-depth look at how to create QR codes, which are becoming increasingly popular in industrial applications and the retail sector. The IronBarcode library, one of the most popular and powerful libraries, will be used to demonstrate how to generate QR codes.
Open Visual Studio > Click on Create New Project > Select Windows Forms Application Template > Press Next > Name the Project > Press Next > Select your Target .NET Framework > Click the Create button.
After creating the project, design the form as follows from the Visual Studio toolbox: PictureBox
, Label
, Textbox
, and Button
controls.
A Windows Forms Application UI to load an image and generate a QR Code
The first step is to install the barcode library. You can do this by using one of the following three methods:
Write the following command in the Package Manager Console. It will download and install the package for you.
Install-Package BarCode
Installation progress in Package Manager Console UI
You can also install the barcode library by using the NuGet Package Solution. Simply follow these steps:
Click on Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
This will open the NuGet Package Manager for you. Click on Browse and search BarCode, then install the class library.
Finding BarCode library in NuGet Package Manager
As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from [.NET Barcode DLL].
For this tutorial, to ensure adequate references, the IronBarCode
namespace along with other system assemblies is necessary.
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Linq
The following sample code allows you to generate a QR code image with just one line of code. Enter the desired text into the text box for which you want to generate a QR code. Place this code in the "Generate PNG" button click event. The QR code barcode images can be saved in PNG format.
// Simple QR Code generation
private void button1_Click(object sender, EventArgs e)
{
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode(textBox1.Text);
qrCode.SaveAsPng("QrCode.png");
}
// Simple QR Code generation
private void button1_Click(object sender, EventArgs e)
{
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode(textBox1.Text);
qrCode.SaveAsPng("QrCode.png");
}
' Simple QR Code generation
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCode(textBox1.Text)
qrCode.SaveAsPng("QrCode.png")
End Sub
Here is the output of the QR code generator:
QR code of: https://ironsoftware.com/csharp/barcode/docs/
By using the CreateQrCodeWithLogo
method from the QRCodeWriter
class, additional information, such as a logo, can be added to the QR code. The sample code illustrates how easy this is.
Browse the logo from your computer, and it will open in PictureBox
. The code goes as follows:
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK) {
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
// store image file path in class data member. Initialize it as string ImageFileName;
ImageFileName = open.FileName;
}
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK) {
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
// store image file path in class data member. Initialize it as string ImageFileName;
ImageFileName = open.FileName;
}
' open file dialog
Dim open As New OpenFileDialog()
' image filters
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp"
If open.ShowDialog() = DialogResult.OK Then
' display image in picture box
pictureBox1.Image = New Bitmap(open.FileName)
' store image file path in class data member. Initialize it as string ImageFileName;
ImageFileName = open.FileName
End If
Next, simply type the text in the text box, place this code in the Generate PNG button, and click.
// Adding a Logo
GeneratedBarcode Qrcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500);
Qrcode.SaveAsPng("QrCodeWithImage.png");
// Adding a Logo
GeneratedBarcode Qrcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500);
Qrcode.SaveAsPng("QrCodeWithImage.png");
' Adding a Logo
Dim Qrcode As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500)
Qrcode.SaveAsPng("QrCodeWithImage.png")
This code adds the Iron logo to the barcode. It automatically sizes it to an appropriate size where the pure code is still readable and aligns that logo to the QR code square grid so that it looks appropriate.
C# Create QR Code With Logo Image
Finally, the generated QR code can be saved as a PDF or HTML image. The final line of code opens the PDF in your default PDF browser for your convenience. Add the SaveAsPdf
in the Generate PDF button and SaveAsHtmlFile
in the Generate HTML button.
// Adding a Logo
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500);
//Save as PDF
qrCode.SaveAsPdf("QRWithLogo.pdf");
//Also Save as HTML
qrCode.SaveAsHtmlFile("QRWithLogo.html");
// Adding a Logo
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500);
//Save as PDF
qrCode.SaveAsPdf("QRWithLogo.pdf");
//Also Save as HTML
qrCode.SaveAsHtmlFile("QRWithLogo.html");
' Adding a Logo
Dim qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500)
'Save as PDF
qrCode.SaveAsPdf("QRWithLogo.pdf")
'Also Save as HTML
qrCode.SaveAsHtmlFile("QRWithLogo.html")
IronBarcode features a friendly API for developers to read and write data to barcodes and QR codes for C# .NET, optimizing accuracy and ensuring a low error rate in real-world cases. For more information about IronBarcode, please visit this documentation website.
Additionally, IronBarcode also supports reading barcodes from images, as well as providing extra options to read barcodes with more accuracy or apply filters to images.
Currently, if you buy the complete Iron Suite, you can get five libraries for the price of just two. Please visit the pricing page for more details.
9 .NET API products for your office documents