How to insert barcode images into the Header or Footer sections of a RDLC ReportViewer in Windows Forms with C# or VB
Product Barcode Professional for Windows Forms Published 10/21/2010 Updated 10/21/2010
Requirements
Neodynamic Barcode Professional 7.0 for .NET Windows Forms (or greater)
Microsoft .NET Framework 3.5 (or greater)
Microsoft Visual Studio 2010 or Visual Basic 2010 Express or Visual C# 2010 Express
In this simple guide you will learn how to insert a barcode image to the Header or Footer sections of a RDLC report in a Windows Forms application project using C# or VB.NET and Neodynamic Barcode Professional for Windows Forms.
RDLC reports are included since Visual Studio 2005 and allow you to design reports in Visual Studio which are displayed on a form by using ReportViewer controls.
- The source code in this guide was made by using Visual Studio 2010 but you can easily use it in older versions of ReportViewer controls like the ones delivered with Visual Studio 2008.
- The RDLC report in this guide has no data source settings for simplicity but the same technique shows here can be used with reports bound to any data source.
The Windows Forms app sample displaying a barcode image inside the Header section of a RDLC report
Please follow up these steps:
- Download and install latest version of Neodynamic Barcode Professional for Windows Forms .NET
- Open Visual Studio and create a new Windows Forms application naming it LocalReportWithBarcodeInHeader
- Add a new RDLC Report item to your project
- With the report displayed at design time, click Report > Report Properties
In the Report Properties dialog box, click Code tab and enter the following function. Then click OK
Public Function GetBarcodeImageHeader(ByVal barcodebase64 As String) As Byte() Return Convert.FromBase64String(barcodebase64) End Function - Add a new parameter to the report naming it BarcodeBinaryContent and the data type for it must be set up to Text
- With the report displayed at design time, click Report > Add Page Header
- Design your report so it looks like the following figure. In it, the main element is the Image control which will be used to display the barcode image generated by Barcode Professional
- Select the Image control on the report and in the Properties Window set the following properties:
- MIMEType to image/png
- Source to Database
- Value to =Code.GetBarcodeImageHeader(Parameters!BarcodeBinaryContent.Value)
- Sizing to AutoSize
- Add a reference to Neodynamic.WinControls.BarcodeProfessional.dll clicking Project > Add Reference...
- Open the form at design time and add a Label, a TextBox, a Button and a ReportViewer control as shown in the following figure:
- On the ReportViewer control and set up the RDLC report created above to it
- Open the form's code behind class and add this method inside it:
VB
Private Sub UpdateBarcodeOnHeader() If (Me.textBox1.Text.Trim = "") Then MessageBox.Show("Please enter a value to encode, thanks!") Return End If Dim barcodeBase64 As String = "" 'Create an instance of Barcode Professional Using bcp As New Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional() 'Barcode settings bcp.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128 bcp.BarcodeUnit = Neodynamic.WinControls.BarcodeProfessional.BarcodeUnit.Inch bcp.BarWidth = 1.0F / 96.0F bcp.BarHeight = 0.5F 'Set the value to encode bcp.Code = Me.textBox1.Text 'Generate the barcode image and convert to base64 string barcodeBase64 = Convert.ToBase64String(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png)) End Using 'Set report parameter Dim param As New Microsoft.Reporting.WinForms.ReportParameter("BarcodeBinaryContent", barcodeBase64, False) Me.ReportViewer1.LocalReport.SetParameters(New Microsoft.Reporting.WinForms.ReportParameter() {param}) Me.ReportViewer1.RefreshReport() End SubC#
private void UpdateBarcodeOnHeader() { if (this.textBox1.Text.Trim() == "") { MessageBox.Show("Please enter a value to encode, thanks!"); return; } string barcodeBase64 = ""; //Create an instance of Barcode Professional using (Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional()) { //Barcode settings bcp.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128; bcp.BarcodeUnit = Neodynamic.WinControls.BarcodeProfessional.BarcodeUnit.Inch; bcp.BarWidth = 1f / 96f; bcp.BarHeight = 0.5f; //Set the value to encode bcp.Code = this.textBox1.Text; //Generate the barcode image and convert to base64 string barcodeBase64 = Convert.ToBase64String(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png)); } //Set report parameter Microsoft.Reporting.WinForms.ReportParameter param = new Microsoft.Reporting.WinForms.ReportParameter("BarcodeBinaryContent", barcodeBase64, false); this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter[] { param }); this.reportViewer1.RefreshReport(); } - Add this code to the Form_Load event handler:
VB
Me.textBox1.Text = "000111222333" UpdateBarcodeOnHeader()C#
this.textBox1.Text = "000111222333"; UpdateBarcodeOnHeader(); - On the form, double click on the button and add this code to the Click event handler:
VB
UpdateBarcodeOnHeader()C#
UpdateBarcodeOnHeader(); - That's it. Run the project. Enter some value to encode and click on Refresh button so the barcode on the report's header be updated.
Sample Files Download
Here are the VB.NET and C# versions of this sample. Please download the zip file and extract it.
- For Visual Basic.NET: LocalReportsWithBarcodeInHeaderFooterVB.zip (296 KB)
- For Visual C#.NET: LocalReportsWithBarcodeInHeaderFooterCS.zip (271 KB)
Remember to download and install Barcode Professional for Windows Forms .NET in order to reproduce this sample demo.
Need help? Contact Support
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.