Quantcast
Viewing all articles
Browse latest Browse all 80

C# WPF Generate and Display QR Code

Download QrCode.Net

Download and install QrCode.Net from NuGet, there are two ways to install it.

Right click your project, select Manage NuGet Packages, in newly opened NuGet Package Manager tab, choose Browse tab and type QrCode.Net in Search text box, and install the QrCode.Net package by clicking the download icon at right side.

Or

Run following command in Package Manager Console

Install-Package QrCode.Net

 

Create Container Window

Create a window with name MainWindow and add an Image control named QrCodeImage into it.

MainWindow.xaml

<Window x:Class="DisplayQRCode.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DisplayQRCode"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image Name="QrCodeImage" />
    </Grid>
</Window>

 

Display QR Code in Window

In MainWindow.xaml.cs MainWindow constructor function, add following code (should be located after InitializeComponent() method)

QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode;
encoder.TryEncode("redino", out qrCode);
WriteableBitmapRenderer wRenderer = new WriteableBitmapRenderer(new FixedModuleSize(2, QuietZoneModules.Two), Colors.Black, Colors.White);
WriteableBitmap wBitmap = new WriteableBitmap(70, 70, 96, 96, PixelFormats.Gray8, null);
wRenderer.Draw(wBitmap, qrCode.Matrix);

QrCodeImage.Source = wBitmap;

 

Running the project you will see the QR Code is displayed in the MainWindow.

 

 

The post C# WPF Generate and Display QR Code appeared first on Redino blog.


Viewing all articles
Browse latest Browse all 80

Trending Articles