QR is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan. This QR code is also used in automobiles. If customer wanst to know about the part details, then only a mobile app is needed to read it. We are using QR code to read all data with security.
A company gives a customer security key for reading QR code. My project came and said I want a QR code with Security key. So I decided make a QR code using C#. I know in C# we have a lot of DLL (library) or Third party software also available in market to make QR codes.
I have created one image to display the QR code in report, this image binds in RDLC report.
Bind image in report.
Figure: Showing how to bind image
One question arises, that this is a static way, not dynamic. But don't worry we can pass image parameters according to our requirement.
// Bind the image dynamically in parameters.
ReportParameter parameter = new ReportParameter(
"QR_Img",
"file:\\" + Application.StartupPath + "test.png",
true
);
Page setting as per requirement.
System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
// Margin Top set here...
pg.Margins.Top = 1;
// Margin bottom set here
pg.Margins.Bottom = 1;
// Margin left set here
pg.Margins.Left = 0;
// Margin right set here
pg.Margins.Right = 0;
reportViewer1.SetPageSettings(pg);
// Hide the print button
this.reportViewer1.ShowPrintButton = false; // if you want print button on show in preview then set True here..
this.reportViewer1.RefreshReport();
// Set the parameters in report viewer
this.reportViewer1.LocalReport.SetParameters(parameter);
// Refresh here... clear all old values..
this.reportViewer1.LocalReport.Refresh();
this.reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
Create the QR code.
// Set information
string code = "Your information you want display in QR code";
QRCodeGenerator qrGenerator = new QRCodeGenerator();
// Create QR code
QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
// Pass the reference in bitmap
using (Bitmap bitMap = qrCode.GetGraphic(20))
{
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
using (MemoryStream ms1 = new MemoryStream(byteImage))
{
img = Image.FromStream(ms1);
}
img = ResizeImage(img, 150, 150);
RectangleF rectangleF = new RectangleF(10f, 0.0f, 0.0f, 0.0f);
Image MyImage = new Bitmap(img);
using (Graphics g2 = Graphics.FromImage(MyImage))
{
IntPtr dc2 = g2.GetHdc();
g2.ReleaseHdc(dc2);
}
// Image save here...
MyImage.Save(Application.StartupPath + "test.png", System.Drawing.Imaging.ImageFormat.Jpeg);
// Change the save path of image... like this way
}
}
Your QR Image is ready to display information.
After completing everything, ready your report for printing.
Above report aligns and sets according to our requirement. We are using TVS LP-45 printer.
I used in this project INI file. So please update your database information in INI file. You can get INI file in bin folder.
I hope all points are clear. If you have any query drop it in the comment section below.
Download demo project.
Read more articles on C# Programming.