Skip to main content

Generate QR Code in WPF

Introduction

In my previous two blogs, we have discussed how to display generate and display various barcodes on the web page. In this blog, we are going to demonstrate how to Generate QR Code in the WPF application.

Getting Started

Here in the demonstration Generate QR Code in WPF, will generate QR code using third party library as there is not inbuilt library provided by Microsoft to generate QR code and will display in WPF image control, In the below, we will see the steps to display QR Code.

net-core-qr-code-generator

As I mentioned in the above paragraph that there is no inbuild library provided by Microsoft to generate QR code, I have taken the help of the ZXing library which is a third party free library and available Nuget. This library provides various options to generate barcodes and QR Code from the user-friendly text.

Generate QR Code in WPF

Here are the steps to generate QR code and let's follow the steps to complete demonstrations.

Demonstration:- Generate QR Code

  1. Open visual studio, go to file manu=>New and clieck on Projct.
  2. The New Project window will be appear, Select 'WPF Application' projct template and name the project as GenerateQRCode. Then click on 'OK' button.
  3. Another window will be appear, just click on 'OK' buttone.
  4. Go to Solution Explorer, right click on your project then click on 'Manage NuGet Packages'.
  5. The 'Manage NuGet Packages' window will appear, click on Browse and search for ZXing.
  6. List of packages will be list out in the list.
  7. Select the 'ZXing.Net' and install it.
  8. barcode generator
    Generate QR Code in WPF
  9. Add reference of System.Drawing library to the project.
  10. Then go to the Solution and open, right click on MainWindow and click on 'View Designer'.
  11. The MainWondow.xaml file will be open there you may find grid control, replace the grid control wth below codes.
  12.   <Grid>  
         <Grid.ColumnDefinitions>  
           <ColumnDefinition Width="200"/>  
           <ColumnDefinition Width="*"/>  
           <ColumnDefinition Width="200"/>  
         </Grid.ColumnDefinitions>  
         <Grid.RowDefinitions>  
           <RowDefinition Height="100"/>  
           <RowDefinition Height="30"/>  
           <RowDefinition Height="40"/>  
           <RowDefinition Height="200"/>  
           <RowDefinition Height="*"/>  
         </Grid.RowDefinitions>  
         <TextBlock Text="QR Code Generator" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" FontSize="30"/>  
         <TextBlock Text="QR Code Content :" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center"/>  
         <TextBox Grid.Column="1" Grid.Row="1" Name="txtbarcodecontent"/>  
         <Button Content="Generate" Name="btnConvert" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Right" Width="100" Click="BtnConvert_Click"/>  
         <TextBlock Text="QR Code :" Grid.Row="3" HorizontalAlignment="Right" VerticalAlignment="Top"/>  
         <StackPanel Grid.Row="3" Grid.Column="1" Orientation="Vertical">  
           <Image Name="imgbarcode"/>  
           <TextBlock Name="tbkbarcodecontent" FontWeight="Bold" HorizontalAlignment="Center"/>  
         </StackPanel>  
       </Grid>  
    

    XAML Codes for Main Window

  13. Your Main window should look like below Image

  14. net-core-qr-code-generator
    Generate QR Code in WPF
  15. Again go to Solution Explorer => Right click on the MainWondow and click on View Code.
  16. Copy the below code which contains function to convert text to QR Code and past it inside MainWidnow class below the controcture of the class.
  17.  private void BtnConvert_Click(object sender, RoutedEventArgs e)  
         {  
           try  
           {  
               System.Drawing.Image img = null;  
               BitmapImage bimg = new BitmapImage();  
               using (var ms = new MemoryStream())  
               {  
                 BarcodeWriter writer;  
                 writer = new ZXing.BarcodeWriter() { Format = BarcodeFormat.QR_CODE };  
                 writer.Options.Height = 80;  
                 writer.Options.Width = 280;  
                 writer.Options.PureBarcode = true;  
                 img = writer.Write(this.txtbarcodecontent.Text);  
                 img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);  
                 ms.Position = 0;  
                 bimg.BeginInit();  
                 bimg.CreateOptions = BitmapCreateOptions.PreservePixelFormat;  
                 bimg.CacheOption = BitmapCacheOption.OnLoad;  
                 bimg.UriSource = null;  
                 bimg.StreamSource = ms;  
                 bimg.EndInit();  
                 this.imgbarcode.Source = bimg;// return File(ms.ToArray(), "image/jpeg");  
                 this.tbkbarcodecontent.Text = this.txtbarcodecontent.Text;  
             }  
           }  
           catch (Exception ex)  
           {  
             MessageBox.Show(ex.Message);  
           }  
         }  
    

    C# Codes for Main Window

  18. Now your are done and run the project.

Related Articles

  1. Generate Barcode in MVC
  2. Generate Barcode in WPF
  3. Generate QR Code in WPF
  4. Display Barcode Image

Summary

In this blog, we demonstrated how to generate QR code in WPF Application, Home you enjoyed it.

Thanks

Comments

Popular posts from this blog

Creating Application in Prism

Introduction This article explains an illustration of creating a windows application in Prism Library(WPF Prism). The solution includes recommended practices and techniques and is the basis for the procedures in Prism. This illustration created in the Visual Studio 2012, It can also developed in the visual studio 2008 and 2010, because wpf supports from .net framework 3.5 to latest version. Microsoft.Practices.Prism.dll. This assembly contains the implementation of the Prism Library core components such as modularity, logging services, communication services, and definitions for several core interfaces. It also contains the implementation of Prism Library components that target WPF applications, including commands, regions, and events. Microsoft.Practices.Prism.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications built with the Prism Library that consume the Unity Application Block. For example, it contains a bootstrapper base class

WPF Binding

Introduction This blog describes WPF Binding and the measure elements exist with binding with XAML example in XAML and code-behind. Getting Started Binding helps in WPF to flow data from one object to another object, the object which fetches data is called source and the object which receives the data is called target. The Object a be a UI control or object of a class that means in binding you can bind a property of a class and property of another control to WPF UI controls as well. XAML Example <TextBox x:Name="sourceText" Grid.Row="0" /> <Button Grid.Row="1" Content="{Binding ElementName=sourceText, Path=Text}"/> Code Example Button btn=new Button(); Binding binding = new Binding("Text"); binding.Source = sourceText; btn.SetBinding(Button.ContentProperty, binding); In WPF binding has some measure properties or elements that we are using while developing application or projects, here we are going to d