How to Generate QR Code in RDLC Report using C#
By FoxLearn 6/20/2024 7:05:08 AM 405
RDLC (Report Definition Language Client-side) reports can include images, which allows us to dynamically generate and display QR codes using a library like QRCoder.
Here’s a step-by-step How to generate QR Code in RDLC Report using C#.
To play the demo, you need to create a new rdlc report, then install the QRCoder
library from NuGet. This library helps in generating QR codes easily.
To install QRCoder
from Manage Nuget Packages, you can right-click on your project in Visual Studio -> Manage NuGet Packages -> Search for QRCoder
-> Install.
You can also run nuget command PM> Install-Package QRCoder
As you know, QRCoder is an open source library that helps you generate qr code, bar code
Open your RDLC report in Visual Studio
From your report tool box, drag an Image control from the Toolbox to your report design where you want the QR code to appear.
Set the Name
property of the image control to something like QRCodeImage
. You can adjust the size and position of the image control.
In your RDLC report, set the image control's Value
property to the field that holds the QR code image bytes. Right-click on the image control -> Properties -> Set Image
property to something like
=First(Fields!Image.Value, "ReportData")
You can design a simple UI allows you to enter a text, then generate and display the qr code to your rdlc report
Adding a Form_Load event handler to load your rdlc report
private void Form6_Load(object sender, EventArgs e) { this.reportViewer1.RefreshReport(); this.reportViewer1.LocalReport.EnableExternalImages = true; }
Adding code to handle the button click event allows you to generate a qr code and display it in the report viewer control
private void button1_Click(object sender, EventArgs e) { // c# generate QR code QRCoder.QRCodeGenerator generator = new QRCoder.QRCodeGenerator(); QRCoder.QRCodeData data = generator.CreateQrCode(textBox1.Text, QRCoder.QRCodeGenerator.ECCLevel.Q); QRCoder.QRCode qR = new QRCoder.QRCode(data); Bitmap bmp = qR.GetGraphic(7); using (MemoryStream ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Bmp); ReportData reportData = new ReportData(); ReportData.QRCodeRow row = reportData.QRCode.NewQRCodeRow(); // Convert Bitmap to byte array to pass it to RDLC row.Image = ms.ToArray(); reportData.QRCode.AddQRCodeRow(row); // Pass the QR code image byte array to the report ReportDataSource reportDataSource = new ReportDataSource(); // Must match the DataSource in the RDLC reportDataSource.Name = "ReportData"; // Replace with your dataset name reportDataSource.Value = reportData.QRCode; reportViewer1.LocalReport.DataSources.Add(reportDataSource); // Refresh report viewer reportViewer1.RefreshReport(); } }
You need to generate qr code to an image, then convert the image to byte array. Finally, add the byte array to your data source
I hope so you can solve the problem add a byte image to RDLC report