Home > .NET Crystal Reports Barcode > .NET, C#, VB.NET Barcode Componenet for Crystal Reports for .NET WinForms

How to Generate Barcode Images in Crystal Reports in .NET Windows Applications



Crystal Reports Barcode Generator Library

QR Code, Data Matrix, Code 128, GS1, EAN/UPC. Unicode text encoding in C# Crystal Reports Windows Forms, WPF desktop application.

Download Now
NuGet Ready
.NET Framework 4.x, 3.x, 2.x


How to create barcode with specified size in C#?

Here we will learn how to create a Windows Forms project, create, print barcodes on the Crystal Reports report on the form. By end of this tutorial, you will get a Crystal Reports desktop application with barcodes printed on the report content.



Prerequisites

  • BarcodeLib.Barcode.CrystalReports.dll
  • Microsoft .NET Framework 4.x
  • Visual Studio 2022
  • Crystal Reports for Visual Studio 2022 Developer Edition


1. Create new Windows Forms project

  1. Open Visual Studio 2022, and Create a new project.
  2. Choose Crystal Reports Application and click Next button.
  3. Input the new Project name as BarcodeLibCrystalReportsBarcodeWinFormsDemo,
    and choose Framework as .NET Framework 4.7.2. Click button Create.
  4. In Crystal Reports Gallery dialog, choose Using the Report Wizard for the new document, and choose Standard for Expert. Click button OK.
  5. Click Finish button. We will create data set later.
  6. Now we have created the Crystal Reports Windows application successfully.


2. Add barcode library

  • Add downloaded BarcodeLib barcode library dll BarcodeLib.Barcode.CrystalReports.dll to the project reference.


3. Add data set

  1. Add a new item DataSet named DataSet1.xsd.
  2. Right click data set DataSet1.xsd, Add a DataTable named DataTable1.
  3. Add two columns to the data table DataTable1.
    Column Data: set property DataType to System.String.
    Column Barcode: set property DataType to System.Byte[].


4. Add barcode to report

  1. Double click CrystalReport1.rpt in the project Solution Explorer to open the report designer in Visual Studio.
  2. Right click the report in the designer, choose Database > Database Expert....
  3. In the popup "Database Expert" dialog,
    Select DataTable1 in ADO.NET DataSets,
    Click button >.
    Click button OK.
  4. In Field Explorer window, drag, add two columns Data and Barcode to the report Details section.
  5. Select the Barcode item in the report designer, change its properties:
    Set property XScaling to 4
    Set property YScaling to 1.5
    Here we will print barcode image with 4-inch width, and 1.5-inch height in the report.


5. C# code to print barcode

  1. Copy and replace the following C# code to file Form1.cs in Visual Studio.

    using BarcodeLib.Barcode;
    using BarcodeLib.Barcode.CrystalReports;
    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.CrystalReports.ViewerObjectModel;
    using CrystalDecisions.Shared;
    using CrystalDecisions.Windows.Forms;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BarcodeLibCrystalReportsBarcodeWinFormsDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Data", typeof(String));
                dt.Columns.Add("Barcode", typeof(byte[]));
    
                String data1 = "BarcodeLib.com";
                String data2 = "Google.com";
    
                LinearCrystal barcode = new LinearCrystal();
                barcode.Type = BarcodeType.CODE128;
                barcode.UOM = UnitOfMeasure.INCH;
                barcode.ImageWidth = 5;
                barcode.ImageHeight = 2;
                barcode.ResizeImagePaddingTransparent = false;
                barcode.ImageFormat = BLImageFormat.PNG;
    
                barcode.Data = data1;
                byte[] imgData = barcode.drawBarcodeAsBytes();
                dt.Rows.Add(data1, imgData);
    
                
                barcode.Data = data2;
                imgData = barcode.drawBarcodeAsBytes();
                dt.Rows.Add(data2, imgData);
    
                //  Create Report
                CrystalReport1 rpt = new CrystalReport1();
                //  Binding DataTable
                rpt.SetDataSource(dt);
                //  Binding Report
                this.crystalReportViewer1.ReportSource = rpt;
            }
        }
    }
    
  2. Add Windows Form Load event handler Form1_Load.


6. Run WinForms application

Now we have finished a new Windows Forms application with barcodes printed on the Crystal Reports report.





Create Barcode Images from database in Crystal Reports for .NET Windows Forms

Prerequisites

  • BarcodeLib.Barcode.CrystalReports.dll
  • Microsoft .NET Framework 2.0 (or later)
  • Crystal Report for Visual Studio .NET 2010


Tutorial

  1. Create a new Windows Application Project.
    • Create a new project using "Crystal Reports Application" as template, named "BarcodeforCrystalReports".
  2. Add a Crystal Report item to the project.
    • Add a new item (Crystal Report), named "CustomerReport.rpt" to the project. view image
    • Select Expert as "Mail Label". Click "OK" button. view image
    • In "Mailing Labels Report Creation Wizard", click "Create New Connection", and expand "ADO.NET". view image
    • In "ADO.NET" form, select "CustomerDataSet.xsd" file in your downloaded package, and click "Finish" button. view image
    • In "Mailing Labels Report Creation Wizard", add table "Customer" under "ADO.NET", to selected table. And click "Next" button. view image
    • Add all three fields "ID", "CustomerId", "CustomerName", to "Fields to Display:". Click "Next". view image
    • Select default label "Return Address (Avery 5267)", and click "Finish" button.
    • Now you can view the report template, and find that field "Barcode" not in the report template. view image
    • Re-arrange the report template. view image
    • Drag field "Barcode" to the report template. And Right click "Barcode" object, select "Format Object" menu. view image
    • In "Format Editor" form, check "Can Grow" property. view image
  3. Add Crystal Report Viewer to the window form.
    • Resize "Form1", and add "CrystalReportViewer" to the form, with the default setting unchanged. view image
    • Add a reference to BarcodeLib.Barcode.CrystalReports.dll.
    • Add the following code to the Form1.cs file.
     using System.Data.OleDb;
using System.Drawing.Imaging;
using BarcodeLib.Barcode.CrystalReports;
using BarcodeLib.Barcode;


private void Form1_Load(object sender, EventArgs e)
{
//create the database connection. Please change to correct data file (BarcodeDemoData.mdb) path.
OleDbConnection aConnection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:/BarcodeLib_NETBarcode_Trial/ReportingData/BarcodeDemoData.mdb"
);aConnection.Open();

OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT * FROM Customer", aConnection);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);

//Add the Barcode column to the DataSet
ds.Tables[0].Columns.Add(new DataColumn("Barcode", typeof(byte[])));

//Create an instance of Linear Barcode
//Use DataMatrixCrystal for Data Matrix
//Use PDF417Crystal for PDF417
//Use QRCodeCrystal for QR Code
LinearCrystal barcode = new LinearCrystal();
//Barcode settings
barcode.Type = BarcodeType.CODE128;
barcode.BarHeight = 50; //50 pixel
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;

foreach (DataRow dr in ds.Tables[0].Rows)
{
barcode.Data = (int)dr["CustomerId"] + "";
byte[] imageData = barcode.drawBarcodeAsBytes();
dr["Barcode"] = imageData;
}

CustomerReport rpt = new CustomerReport();
rpt.SetDataSource(ds);

this.crystalReportViewer1.ReportSource = rpt;

//close the connection Its important.
aConnection.Close();

}
3. Run the project.
Related .NET Barcode Generator Controls