Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This tutorial will use IronBarcode as a core library to generate QR codes that have many applications in different industries.
The proceeding sections of this article present an example to demonstrate how to generate QR codes easily.
Open Visual Studio, and select New Project from the File Menu.
Choose the Console App template in the window that appears, and click Next.
Creating a New Console App in Visual Studio for QR Code Generation
In the Project name text field, type in any project name to your liking (for example, QR Code Generator), and specify a location for the new project in the Location field. Afterward, click the Next button to continue.
Choosing a name and location for a new Console Application in Visual Studio to create QR codes
Choose a .NET Framework from the Framework dropdown menu (here, we are using .NET 6.0 (Long term support)) and click Create.
Creating the new Console App in Visual Studio under .NET 6.0 Framework
You can download and install the IronBarcode library in four ways.
These ways are:
From the menu bar, go to Tools > NuGet Package Manager > Manage NuGet Packages for solution... to open the Package Manager UI.
Installing the IronBarcode library using Visual Studio's NuGet Package Manager UI
Alternatively, you can right-click the name of your project from the Solution Explorer Window, and select Manage NuGet Packages... from the context menu.
Click on the Browse tab, and type in Barcode in the search field. Select IronBarcode from the list of related packages (shown as the first result in the image below), choose your project in the pane on the right and click the Install button.
Searching for the IronBarcode library in the Package Manager UI. It will most likely appear before all other libraries in the search results
Go to Tools > NuGet Package Manager > Package Manager Console. Enter the following command in the Command-line panel that appears and press ENTER:
Install-Package BarCode
The above command will download and install the library into the current project.
Search for the Barcode library page on the NuGet Gallery website in your browser, (or click on this NuGet BarCode
package link to access the page directly).
Click on the Download package link from the menu on the right-hand side to save the library on your computer. Next, double-click the downloaded library from your File Manager to install it into your project automatically. Finally, reload your project, and it will be ready to go.
Click on IronBarcode's homepage to download the latest .NET barcode DLL. Once downloaded, follow the steps below to add the package to your project:
Adding the IronBarcode DLL into the project directly from Visual Studio
Inserting the IronBarcode DLL as a new COM reference in your project
Generate a new QR code by invoking the CreateQrCode
method from the QRCodeWriter
class:
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("MyQR.png");
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("MyQR.png");
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("MyQR.png")
The CreateQrCode
method accepts one required parameter, which is the data to be encoded in the code image (can be a String or a Stream). The method also accepts three optional parameters:
CreateQrCode
uses the highest correction level QRCodeWriter.QrErrorCorrectionLevel.Highest
The example above generates a 500-pixel by 500-pixel graphic using the medium level of error correction. The subsequent call to the SaveAsPng
method on the generated QR code saves it as a PNG file at a given file location.
The result of calling QrCodeWriter.CreateQrCode using the aforementioned parameters
Next, the sample code below uses the CreateQrCodeWithLogo
method to add a company logo to a generated QR code, a typical use-case scenario for any business.
var qrWithLogo = QRCodeWriter.CreateQrCodeWithLogo("Hello World", "qrlogo.png",500);
qrWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkRed);
qrWithLogo.SaveAsPng("Logo_QR_Code.png");
var qrWithLogo = QRCodeWriter.CreateQrCodeWithLogo("Hello World", "qrlogo.png",500);
qrWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkRed);
qrWithLogo.SaveAsPng("Logo_QR_Code.png");
Dim qrWithLogo = QRCodeWriter.CreateQrCodeWithLogo("Hello World", "qrlogo.png",500)
qrWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkRed)
qrWithLogo.SaveAsPng("Logo_QR_Code.png")
In the example above, a String value of "Hello, World" is encoded into a new QR code that embeds an image located at a specified file path. The image is automatically sized to fit the QR code square grid, being aligned to the size that will allow QR Code readers to still read the pure code data.
The next line of code above uses the ChangeBarCodeColor
method to change the color of the QR code to dark red. Here, we do the coloring using one of the available System color class types provided by C# (namely, System.Drawing.Color.DarkRed). It is possible to specify colors in the HTML hex color notation, as the line of code below shows:
qrWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));
qrWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));
qrWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"))
The last line of code in the code example above calls the SaveAsPng method to save the QR code as a PNG file. QR codes can be saved in other file formats such as HTML:
qrWithLogo.SaveAsHtmlFile("test.html");
qrWithLogo.SaveAsHtmlFile("test.html");
qrWithLogo.SaveAsHtmlFile("test.html")
Refer to this documentation page for a full list of file types to which QR codes can be saved.
Generate QR Codes in different colors, in different file formats, and using different images the methods available in IronBarcode's QRCodeWriter class
The line of code demonstrates how IronBarcode can be used within a .NET MVC Web Application:
public IActionResult Index()
{
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPdf("Demo.png");
return File("Demo.png", "image/png", "Demo.png", true);
}
public IActionResult Index()
{
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPdf("Demo.png");
return File("Demo.png", "image/png", "Demo.png", true);
}
Public Function Index() As IActionResult
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPdf("Demo.png")
Return File("Demo.png", "image/png", "Demo.png", True)
End Function
The example above operates very similarly to the previous examples. First, a QR code is generated, and then it's returned to the client in the response body.
IronBarcode is one of the fastest and the most powerful libraries available for reading and writing barcodes. This simple library is supported by various operating systems, it supports many barcode formats and it is easy to use.
The licensing and legal information for using IronBarcode can be found on the licensing page. IronBarcode is free to use under the Free Developer License. The premium (paid) developer licensing includes one year of free support and product updates.
9 .NET API products for your office documents