Print QRCode image using C# and VB.Net in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

in the run mode i have textbox and type the value when i click Generate QR code, QR code is generated.

i want to print QR Code for this how to do write the code.

Below is the code for generating QR Code

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            string code = txtplant.Text + "," + txtprotype.Text + "," + txtstore.Text + "," + txtvendor.Text + "," + txtmaterial.Text;

            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            imgBarCode.Height = 150;
            imgBarCode.Width = 150;
            using (Bitmap bitMap = qrCode.GetGraphic(20))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byteImage = ms.ToArray();
                    imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                }
                plBarCode.Controls.Add(imgBarCode);
            }

 

Posted 3 months ago

Hi narasiman,

Put the PlaceHolder inside a Panel or DIV and the refer the following article for print.

Print ASP.Net Panel contents in ASP.Net using C# VB.Net and JavaScript

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate QR Code" OnClick="GenerateQRCode" />
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="return PrintPanel();" />
<hr />
<asp:Panel ID="pnlContents" runat="server">
    <asp:PlaceHolder ID="plBarCode" runat="server" />
</asp:Panel>
<script type="text/javascript">
    function PrintPanel() {
        var panel = document.getElementById("<%=pnlContents.ClientID %>");
        var printWindow = window.open('', '', 'height=400,width=800');
        printWindow.document.write('<html><head><title>DIV Contents</title>');
        printWindow.document.write('</head><body >');
        printWindow.document.write(panel.innerHTML);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        setTimeout(function () {
            printWindow.print();
        }, 500);
        return false;
    }
</script>

Namespaces

C#

using System.Drawing;
using System.IO;
using QRCoder;

VB.Net

Imports System.Drawing
Imports System.IO
Imports QRCoder

Code

C#

protected void GenerateQRCode(object sender, EventArgs e)
{
    string code = txtCode.Text;
    QRCodeGenerator qrGenerator = new QRCodeGenerator();
    QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
    System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
    imgBarCode.Height = 150;
    imgBarCode.Width = 150;
    using (Bitmap bitMap = qrCode.GetGraphic(20))
    {
        using (MemoryStream ms = new MemoryStream())
        {
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteImage = ms.ToArray();
            imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
        }
        plBarCode.Controls.Add(imgBarCode);
    }
}

VB.Net

Protected Sub GenerateQRCode(ByVal sender As Object, ByVal e As EventArgs)
    Dim code As String = txtCode.Text
    Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()
    Dim qrCode As QRCodeGenerator.QRCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q)
    Dim imgBarCode As System.Web.UI.WebControls.Image = New System.Web.UI.WebControls.Image()
    imgBarCode.Height = 150
    imgBarCode.Width = 150
    Using bitMap As Bitmap = qrCode.GetGraphic(20)
        Using ms As MemoryStream = New MemoryStream()
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
            Dim byteImage As Byte() = ms.ToArray()
            imgBarCode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(byteImage)
        End Using
        plBarCode.Controls.Add(imgBarCode)
    End Using
End Sub

Screenshot