Generate Barcode in WPF

Kailash Chandra Behera | Saturday, May 30, 2020

Introduction

In this blog, we are going to demonstrate how to Generate Barcode in the WPF application. Here we will create a barcode generator in WPF application and will create barcode at runtime by taking input from your interface.

Getting Started

Here in the demonstration Generate Barcode in WPF, will use a third party barcode image generator library as there is not inbuilt library provided by Microsoft to generate Bar code and will display in WPF image control, In the below, we will see the steps to display Barcode.

barcode image generator

As I mentioned in the above paragraph that there is no inbuild library provided by Microsoft to generate Bar 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 Barcode from the user-friendly text.

Barcode Generator

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

Demonstration:- Generate Barcode

  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 GenerateBarCode. 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. how to generate barcode in csharp
    Barcode Generator
  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="Barcode Generator" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" FontSize="30"/>  
         <TextBlock Text="Barcode 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="Barcode :" 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 barcode generator
    Generate Barcode 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 Barcode 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.CODE_93};  
                 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.

In this blog, we demonstrated how to generate Barcode in WPF Application, Hope you enjoyed it.

Thanks