Friday, 3 March 2023

zatca qr code C# AspNet | QR Code ZATCA AspNet WebForm | zatca qr gene...



Zatca.aspx  UI QR CodeFile Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="zatca.aspx.cs" Inherits="ManojKallaTutorial.zatca" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:Image ID="imgQrCode" runat="server" />
            <br />
        <br />  
        <asp:Button ID="btnSubmit" runat="server" Text="Generate QR Code"  OnClick="btnSubmit_Click"/>
    </div>
    </form>
</body>
</html>






Zatca.aspx.cs  QR CodeFile Code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ManojKallaTutorial
{
    public partial class zatca : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string SellerName = "Abudl Khalid Yeman";
            string TaxNo = "191786123125987";
            DateTime DtVal = DateTime.Now;
            Double TotalAmt = 100000;
            Double TaxAmt = 1000;
            string GenFile = GenerateQrCode(SellerName, TaxNo, DtVal, TotalAmt, TaxAmt);
            imgQrCode.ImageUrl = "http://localhost:50358/QR/"+GenFile;
       }

        public string GenerateQrCode(string SellerName, string TaxNo, DateTime DtTime,Double Total, Double Tax )
        {
            string FileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".png";
            TLVCls tlv = new TLVCls(SellerName, TaxNo, DtTime, Total, Tax);
            System.Drawing.Image img =  tlv.toQrCode();
            img.Save(@"C:\Users\Admin\Documents\visual studio 2015\Projects\EFDemoWebForm\Entity\QR\" + FileName, System.Drawing.Imaging.ImageFormat.Png);
            return FileName;
        }


    }
}




TLVCls Class File Code:


using System;
using System.Text;
using System.Drawing;
using System.Collections.Generic;
using ZXing;
using ZXing.Common;

namespace ManojKallaTutorial
{
    public class TLVCls
    {
        byte[] Seller;
        byte[] VatNo;
        byte[] dateTime;
        byte[] Total;
        byte[] Tax;

        public TLVCls(String Seller, String TaxNo, DateTime dateTime, Double Total, Double Tax)
        {
            this.Seller = Encoding.UTF8.GetBytes(Seller);
            this.VatNo = Encoding.UTF8.GetBytes(TaxNo);

            this.dateTime = Encoding.UTF8.GetBytes(dateTime.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
            this.Total = Encoding.UTF8.GetBytes(Total.ToString());
            this.Tax = Encoding.UTF8.GetBytes(Tax.ToString());
        }

        private String getasText(int Tag, byte[] Value)
        {
            return (Tag).ToString("X2") + (Value.Length).ToString("X2") + BitConverter.ToString(Value).Replace("-", string.Empty);
        }

        private byte[] getBytes(int id, byte[] Value)
        {
            byte[] val = new byte[2 + Value.Length];
            val[0] = (byte)id;
            val[1] = (byte)Value.Length;
            Value.CopyTo(val, 2);
            return val;
        }

        private String getString()
        {
            String TLV_Text = "";
            TLV_Text += this.getasText(1, this.Seller);
            TLV_Text += this.getasText(2, this.VatNo);
            TLV_Text += this.getasText(3, this.dateTime);
            TLV_Text += this.getasText(4, this.Total);
            TLV_Text += this.getasText(5, this.Tax);
            return TLV_Text;
        }

        public override string ToString()
        {
            return this.getString();
        }

        public String ToBase64()
        {
            List<byte> TLV_Bytes = new List<byte>();
            TLV_Bytes.AddRange(getBytes(1, this.Seller));
            TLV_Bytes.AddRange(getBytes(2, this.VatNo));
            TLV_Bytes.AddRange(getBytes(3, this.dateTime));
            TLV_Bytes.AddRange(getBytes(4, this.Total));
            TLV_Bytes.AddRange(getBytes(5, this.Tax));
            return Convert.ToBase64String(TLV_Bytes.ToArray());
        }

        public Bitmap toQrCode(int width = 250, int height = 250)
        {

            BarcodeWriter barcodeWriter = new BarcodeWriter
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new EncodingOptions
                {
                    Width = width,
                    Height = height
                }
            };
            Bitmap QrCode = barcodeWriter.Write(this.ToBase64());

            return QrCode;
        }


    }

}

No comments:

Post a Comment

Please, Write your valuable comments..