Create QR Code Report Using RDLC Report With Preview

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.
  1. // Bind the image dynamically in parameters.  
  2. ReportParameter parameter = new ReportParameter("QR_Img""file:\\" + Application.StartupPath + "test.png", true);  
Page setting as per requirement:
  1. System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();    

  2. //Margin Top set here...    
  3. pg.Margins.Top = 1;    

  4. // Margin bottom set here  
  5. pg.Margins.Bottom = 1;  
  6.   
  7. // Margin left set here  
  8. pg.Margins.Left = 0;    

  9. // Margin right set here  
  10. pg.Margins.Right = 0;  
  1. reportViewer1.SetPageSettings(pg);      

  2. // hide the print button       
  3. this.reportViewer1.ShowPrintButton = false;    // if you want print button on show in preview then set True here..  
  4.   
  5. this.reportViewer1.RefreshReport();  
  6.     
  7. // set the parameters in reportviwer       
  8. this.reportViewer1.LocalReport.SetParameters(parameter);     
  9.  
  10. // refres here ... clear all old values ..      
  11.   
  12. this.reportViewer1.LocalReport.Refresh();      
  13.   
  14. this.reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);   
Create the QR code:
  1. // set information   
  2. string code = " Your information you want display in QR code";  
  3.   
  4. QRCodeGenerator qrGenerator = new QRCodeGenerator();    
  5.  // Create QRcode....    
  6.   
  7.  QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);    
  8.  //Pass the reference in bitMAP...    
  9.   
  10.  using (Bitmap bitMap = qrCode.GetGraphic(20))    
  11.  {    
  12.      using (MemoryStream ms = new MemoryStream())    
  13.      {    
  14.          bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);    
  15.   
  16.          byte[] byteImage = ms.ToArray();    
  17.          MemoryStream ms1 = new MemoryStream(byteImage);    
  18.          img = Image.FromStream(ms1);    
  19.   
  20.   
  21.      }    
  22.      img = ResizeImage(img, 150, 150);    
  23.   
  24.      RectangleF rectangleF = new RectangleF(10f, 0.0f, 0.0f, 0.0f);    
  25.   
  26.      Image MyImage = new Bitmap(img);    
  27.   
  28.      Graphics g2 = Graphics.FromImage(MyImage);    
  29.   
  30.      IntPtr dc2 = g2.GetHdc();    
  31.   
  32.      g2.ReleaseHdc(dc2);    
  33.      //Image save here...     
  34.      MyImage.Save(Application.StartupPath + "test.png", System.Drawing.Imaging.ImageFormat.Jpeg);     
  35. 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: