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.
-
- 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();
-
- pg.Margins.Top = 1;
-
- pg.Margins.Bottom = 1;
-
-
- pg.Margins.Left = 0;
-
- pg.Margins.Right = 0;
- reportViewer1.SetPageSettings(pg);
-
- this.reportViewer1.ShowPrintButton = false;
-
- this.reportViewer1.RefreshReport();
-
-
- this.reportViewer1.LocalReport.SetParameters(parameter);
-
-
-
- this.reportViewer1.LocalReport.Refresh();
-
- this.reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
Create the QR code:
-
- string code = " Your information you want display in QR code";
-
- QRCodeGenerator qrGenerator = new QRCodeGenerator();
-
-
- QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
-
-
- using (Bitmap bitMap = qrCode.GetGraphic(20))
- {
- using (MemoryStream ms = new MemoryStream())
- {
- bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
-
- byte[] byteImage = ms.ToArray();
- 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);
-
- Graphics g2 = Graphics.FromImage(MyImage);
-
- IntPtr dc2 = g2.GetHdc();
-
- g2.ReleaseHdc(dc2);
-
- MyImage.Save(Application.StartupPath + "test.png", System.Drawing.Imaging.ImageFormat.Jpeg);
- hange 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: