using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RDNIDWRAPPER;
using System.Drawing.Imaging;
using System.IO;


namespace NIDCardCS
{
     public partial class Form1 : Form
    {
        RDNIDWRAPPER.RDNID mRDNIDWRAPPER = new RDNIDWRAPPER.RDNID();
        private int counter = 15;
        public static string GetCurrentExecutingDirectory(System.Reflection.Assembly assembly)
        {
            string filePath = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;
            return Path.GetDirectoryName(filePath);
        }
        private static string cTemp;
        private static string Err4 = "รบกวนถ่ายรูปส่วนนี้ไว้และรูปก่อนหน้านี้";
        private static string Err5 = "และติดต่อที่ บริษัท R&D จากเวบที่เปิดต่อไป";
        private static  int  xSaveText = 0;  //   เก็บเป็นข้อความ
        private static int xSaveFace = 0;  //    เก็บรูปหน้า
        private static int xSaveDeskT = 0;  //   เก็บDeskTop
        private static int xFirst = 0;  //   ตัด เลื่อนแนวตั้ง ซ้าย  
        private static int xLast = 0; //  ขยับจากทางซ้าย
        private static int yFirst = 0; //   ขยับขึ้นบน
        private static int yLast = 0; //  ตัดล่าง
        private static Int32 fwidth = 500; //  กว้าง
        private static Int32 fheight = 300; //  ยาว

        public Form1()
        {
            InitializeComponent();
            string fileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\RDNIDLib.DLX";
            if (System.IO.File.Exists(fileName) == false) { Util.Program.Pok_Mess("RDNIDLib.DLX not found");}
            System.Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            int nres = RDNID.OpenNIDLib(fileName); //   สำหรับเรียก 
            if (nres!=0) {
                String m;
                m = String.Format(" error no {0} ", nres);
                Util.Program.SoundOpen("Error.Wav");
                Util.Program.Pok_Mess("1:"+m);
            }
           // string ss=  RDNID.getSoftwareInfoRD();  // ตรวจสอบการตั้งค่าต่างๆ
            ListCardReader();
        }

        private void ListCardReader()
        {
            String[] readlist = RDNID.getReaderListRD();
            if (readlist != null) {
                    for (int i = 0; i < readlist.Length; i++)
                    m_ListReaderCard.Items.Add(readlist[i]);
                    m_ListReaderCard.SelectedIndex = 0;}
            }

        String _yyyymmdd_(String d)
        {
            string s = "";
            string _yyyy = d.Substring(0, 4);
            string _mm = d.Substring(4, 2);
            string _dd = d.Substring(6, 2);
            string[] mm = { "", "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค." };
            string _tm = "-";
            if (_mm == "00") { _tm = "-"; } else { _tm = mm[int.Parse(_mm)];}
            if (_yyyy == "0000") 
                _yyyy = "-";

            if (_dd == "00")
                _dd = "-";

            s = _dd + " " + _tm + " " + _yyyy;
            return s;
        }


        enum NID_FIELD
        {
            NID_Number,   //1234567890123#

            TITLE_T,    //Thai title#
            NAME_T,     //Thai name#
            MIDNAME_T,  //Thai mid name#
            SURNAME_T,  //Thai surname#

            TITLE_E,    //Eng title#
            NAME_E,     //Eng name#
            MIDNAME_E,  //Eng mid name#
            SURNAME_E,  //Eng surname#

            HOME_NO,    //12/34#
            MOO,        //10#
            TROK,       //ตรอกxxx#
            SOI,        //ซอยxxx#
            ROAD,       //ถนนxxx#
            TUMBON,     //ตำบลxxx#
            AMPHOE,     //อำเภอxxx#
            PROVINCE,   //จังหวัดxxx#

            GENDER,     //1#			//1=male,2=female

            BIRTH_DATE, //25200131#	    //YYYYMMDD 
            ISSUE_PLACE,//xxxxxxx#      //
            ISSUE_DATE, //25580131#     //YYYYMMDD 
            EXPIRY_DATE,//25680130      //YYYYMMDD 
            ISSUE_NUM,  //12345678901234 //14-Char
            END            
        };     
        
        protected int ReadCard()

        {
            String strTerminal = m_ListReaderCard.GetItemText(m_ListReaderCard.SelectedItem);
            IntPtr obj = mRDNIDWRAPPER.selectReader(strTerminal );
            Int32 nInsertCard = mRDNIDWRAPPER.isCardInsert();

            if (Program.ExPireForRead(ref nInsertCard)) {  }

            if (nInsertCard != 0)
            {
                this.TopMost = false;
                WindowState = FormWindowState.Maximized;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.ControlBox = true;
                String m, cfErrorText;
                m = String.Format(" error no {0} ", nInsertCard);
                cfErrorText = "";
                switch (nInsertCard.ToString())
                {
                    case ("-11"):
                        cfErrorText = "ติดต่ออุปกรณ์ไม่ได้";
                        break;
                    case ("-1"):
                        cfErrorText = "ไม่ได้เสียบบัตร หรือ เสียบบัตรผิดด้าน";
                        break;
                }
                Util.Program.SoundOpen("Error.Wav");
                // บันทึก Error   MessageBox.Show(m);  
                using (StreamWriter HistoryTxt = File.AppendText(NIDCardCS.Program.History4IDcard))
                {
                    cTemp = Util.Program.DTN(4) + "  ❌ Error No. " + nInsertCard + " 💢" + cfErrorText;
                    HistoryTxt.WriteLine(cTemp);
                    cTemp = "";
                    HistoryTxt.Close(); HistoryTxt.Dispose();
                }
                timer1.Stop();
                Util.Program.ReaderError("พบ Error หมายเลข " + nInsertCard,"Error : " + cfErrorText + " (มาจากตัวแปร cfErrorText) ","", Err4, Err5);
                timer1.Start();
                if (File.Exists(@"D:\BdPosVFP\hoVFP.exe")) { Util.Program.OpenWebSite("https://www.rd-comp.com/contact"); }
                mRDNIDWRAPPER.disconnectCard();
                mRDNIDWRAPPER.deselectReader();
                if (nSample.Checked == false) { cbutton_OK.Text = ""; cbutton_OK.Enabled = false; m_txtFullNameT.Text = "Error"; }
                return 0;
            }
            else { cbutton_OK.Text = "ตกลง"; cbutton_OK.Enabled = true; }

            String NIDNum = mRDNIDWRAPPER.getNIDNumber();      //Read NID Number
            String NIDData = mRDNIDWRAPPER.getNIDText();       //Read all data include NID Number
            if (NIDData == "") { Util.Program.SoundOpen("Error.Wav"); Util.Program.Pok_Mess("3:"+"Read Text error"); }
            else {
                Util.Program.SoundOpen("Ok.Wav");
                string[] fields = NIDData.Split('#');
                m_txtID.Text = NIDNum;   
                String fullname=fields[(int)NID_FIELD.TITLE_T]+" "+fields[(int)NID_FIELD.NAME_T]+" "+fields[(int)NID_FIELD.MIDNAME_T]+" "+fields[(int)NID_FIELD.SURNAME_T];
                m_txtFullNameT.Text=fullname;
                fullname=fields[(int)NID_FIELD.TITLE_E]+" "+fields[(int)NID_FIELD.NAME_E]+" "+fields[(int)NID_FIELD.MIDNAME_E]+" "+fields[(int)NID_FIELD.SURNAME_E];
                m_txtFullNameE.Text=fullname;
                m_txtBrithDate.Text=_yyyymmdd_(fields[(int)NID_FIELD.BIRTH_DATE]);
                m_txtAddress.Text = fields[(int)NID_FIELD.HOME_NO] + "   " + fields[(int)NID_FIELD.MOO] + "   " + fields[(int)NID_FIELD.TROK] + "   " + fields[(int)NID_FIELD.SOI];
                m_txtAddress.Text= m_txtAddress.Text+"   "+fields[(int)NID_FIELD.TUMBON]+"   "+fields[(int)NID_FIELD.AMPHOE]+"   "+fields[(int)NID_FIELD.PROVINCE]+"   ";
                if (fields[(int)NID_FIELD.GENDER] == "1")
                {m_txtGender.Text = "ชาย"; } else {m_txtGender.Text = "หญิง"; }
                m_txtIssueDate.Text = _yyyymmdd_(fields[(int)NID_FIELD.ISSUE_DATE]);
                m_txtExpiryDate.Text = _yyyymmdd_(fields[(int)NID_FIELD.EXPIRY_DATE]);
                if ("99999999" == m_txtExpiryDate.Text)
                    m_txtExpiryDate.Text = "99999999 ตลอดชีพ";
                    m_txtIssueNum.Text = fields[(int)NID_FIELD.ISSUE_NUM];
                    if (m_txtID.Text == "3329900363321") {m_txtID.Text= "3329900366666"; m_txtBrithDate.Text = "01 ส.ค. 2599"; }
            }
            byte[] byteImage = mRDNIDWRAPPER.getNIDPicture();
            if (byteImage == null) { Util.Program.Pok_Mess("4:"+"Read Photo error"); }
            else {
                Image img = Image.FromStream(new MemoryStream(byteImage));
                Bitmap MyImage = new Bitmap(img, m_picPhoto.Width-2, m_picPhoto.Height-2);           
                m_picPhoto.Image = (Image)MyImage; }
                mRDNIDWRAPPER.disconnectCard();
                mRDNIDWRAPPER.deselectReader();
                return 0;
            }

        private void OnReadCardClick(object sender, EventArgs e)
        {
            m_txtID.Text = "";
            m_txtFullNameT.Text = "";
            m_txtFullNameE.Text = "";
            m_txtAddress.Text = "";
            m_txtBrithDate.Text = "";
            m_txtGender.Text = "";
            m_txtIssueDate.Text = "";            
            m_txtExpiryDate.Text = "";
            m_txtIssueNum.Text = "";
            // m_picPhoto.Image = Image.FromFile("blank.jpg");
            ReadCard();
            ReadHelpText();
        }

        private void cbutton_cancel_Click(object sender, EventArgs e)
        {
            Rd4Exit(2,"");
            Application.Exit();
        }

        private void cbutton_OK_Click(object sender, EventArgs e)
        {
            Ok2Save();  
            Rd4Exit(1,"");
            if (NIDCardCS.Program.cpFlag!="L") {Application.Exit();} // ทำงานแบบต่อเนื่อง
                
        }

        private void Rd4Exit(int nOption,string cftext)  // ออกจากโปรแกรมโดยสมบูรณ์
        {
            if (File.Exists(@"_Card\FileHave.opn")) { File.Delete(@"_Card\FileHave.opn"); }
            TextWriter WriteTxt = new StreamWriter(@"_Card\RdCardAnswer.txt");
            switch (nOption)
            {
                case 1: WriteTxt.Write("1   ");  break;
                case 3: WriteTxt.Write("2    🎫❌ กด OK แล้วทำงานไม่สำเร็จ "); break;
                default: WriteTxt.Write("2   "); break;
            }
            WriteTxt.Write("          1=ตกลง,2=ยกเลิก  ");
            if (cftext != "") { WriteTxt.Write("==>"+cftext); }
            WriteTxt.WriteLine(Util.Program.DTN(4));
            WriteTxt.Close(); 
        }



        private void Ok2Save() 
        {
            int nWork = 1; // เช็คการทำงานแบบต่อเนื่อง
            try {
                if (xSaveText == 1 && nWork==1) { Save4Text(); }     // เก็บข้อความ 
                if (xSaveFace == 1 && nWork == 1) { Save4FaceIMG(); }  // เก็บเฉพาะหน้า ได้ไฟล์ ชื่อ _Card\Face.jpg
                if (xSaveDeskT == 1 && nWork == 1) { Save4DeskTOP(); }  // เก็บ Desktop  ได้ไฟล์ ชื่อ  _Card\Desktop.jpg
                if (nWork == 0) { this.Close(); }
            }
            catch (Exception ex)
            {
                nWork = 0; // หยุดการทำงาน
                Rd4Exit(3,ex.ToString());
                Util.Program.ReaderError("ทำงานไม่สำเร็จ","ตอนกด OK",ex.ToString(),"","");  }
            }


        private void Form1_Load(object sender, EventArgs e)
        {
            if (NIDCardCS.Program.inharddisk) { nSample.Checked = true; }
            timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 1000; // 1 second
            timer1.Start();
            cTimer.Text = counter.ToString();

            m_txtID.Text = "";
            m_txtFullNameT.Text = "";
            m_txtFullNameE.Text = "";
            m_txtAddress.Text = "";
            m_txtBrithDate.Text = "";
            m_txtGender.Text = "";
            m_txtIssueDate.Text = "";
            m_txtExpiryDate.Text = "";
            m_txtIssueNum.Text = "";

            m_TxtDetail_1.Text = "";
            m_TxtDetail_2.Text = "";
            m_TxtDetail_3.Text = "";
            m_TxtDetail_4.Text = "";
            m_TxtDetail_5.Text = "";
            m_TxtDetail_6.Text = "";
            m_TxtDetail_7.Text = "";
            // if (!NIDCardCS.Program.insource)  {   // Pipop ใช้งานจริงให้ เอาออก
                this.TopMost = true;
                WindowState = FormWindowState.Maximized;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.ControlBox = true; 
            // } 
            string[] readText = File.ReadAllLines("RdCard.ini"); 
            xSaveText= int.Parse(readText[0].Trim().Substring(0,3))  ;  // เก็บเป็นข้อความ
            xSaveFace= int.Parse(readText[1].Trim().Substring(0,3))  ;  // เก็บรูปหน้า
            xSaveDeskT= int.Parse(readText[2].Trim().Substring(0,3))  ;  // เก็บDeskTop
            xFirst = Int32.Parse(readText[3].Trim().Substring(0,6));    //   ตัด เลื่อนแนวตั้ง ซ้าย  
            xLast = Int32.Parse(readText[4].Trim().Substring(0, 6)); ; //  ขยับจากทางซ้าย
            yFirst = Int32.Parse(readText[5].Trim().Substring(0, 6)); ; //   ขยับขึ้นบน
            yLast = Int32.Parse(readText[6].Trim().Substring(0, 6)); ; //  ตัดล่าง
            fwidth = Int32.Parse(readText[7].Trim().Substring(0, 6)); ; //  กว้าง
            fheight = Int32.Parse(readText[8].Trim().Substring(0, 6)); ; //  ยาว
            SampleTest();
            ReadCard();
            ReadHelpText();
        }



        private void ReadHelpText()
        {
            string cfDirectory, cfBackup, LineText;
            int counter = 1;
            cfDirectory = NIDCardCS.Program.Folder4IDcard + "ช่วยงาน";
            cfBackup = cfDirectory + @"\" + m_txtID.Text.Trim() + ".txt";
            if (File.Exists(cfBackup))
            {
                StreamReader HelpFile = new StreamReader(cfBackup);
                while ((LineText = HelpFile.ReadLine()) != null)
                {
                    switch (counter)
                    {
                        case (1): m_TxtDetail_1.Text = LineText;break;
                        case (2): m_TxtDetail_2.Text = LineText; break;
                        case (3): m_TxtDetail_3.Text = LineText; break;
                        case (4): m_TxtDetail_4.Text = LineText; break;
                        case (5): m_TxtDetail_5.Text = LineText; break;
                        case (6): m_TxtDetail_6.Text = LineText; break;
                        case (7): m_TxtDetail_7.Text = LineText; break;
                    }
                    counter++;
                }
                HelpFile.Close();
            }
            else  {
                m_TxtDetail_1.Text = "";
                m_TxtDetail_2.Text = "";
                m_TxtDetail_3.Text = "";
                m_TxtDetail_4.Text = "";
                m_TxtDetail_5.Text = "";
                m_TxtDetail_6.Text = "";
                m_TxtDetail_7.Text = "";
            }
        }

        

        private void SampleTest()  // แสดงตัวอย่าง   
        {
            if (nSample.Checked==true) {  // Pipop
                Random rnd = new Random();
                string samplename= rnd.Next(1000001,9999999).ToString().Trim();
                string samplesub = rnd.Next(100001, 999999).ToString().Trim();
                m_txtID.Text = samplename + samplesub;
                m_txtFullNameT.Text = "นาย"+samplename+" นามสกุล"+ samplesub;
                m_txtFullNameE.Text = "Mr." + samplename + " Subname" + samplesub;
                m_txtAddress.Text = "36 ซอยXXXXXX ตำบลYYYYYY อำเภอTTTTTT จังหวัดZZZZZZ";
                m_txtBrithDate.Text = "31 ส.ค. 2551";
                m_txtGender.Text = "ชาย";
                m_txtIssueDate.Text = "4 ก.ย. 2551";
                m_txtExpiryDate.Text = "30 ส.ค. 2551";
                m_txtIssueNum.Text = "1196-02-09041306";
                if (File.Exists(@"_Card\Test.jpg")) { m_picPhoto.Image = Image.FromFile(@"_Card\Test.jpg"); }
            }
        }

 
        private void Save4Text()  
        {
            string cfFileText, cfFileTextVFP;
            cfFileText = @"_Card\RDcardUTF8.txt"; 
            cfFileTextVFP=@"_Card\RDcard.txt";  // สำหรับโปรแกรมดึงไปใช้งาน 

            TextWriter RDcard;
            RDcard = new StreamWriter(cfFileText);  // เป็น โค้ด UTF-8 RDcardUTF8.txt
            RDcard.WriteLine(m_txtID.Text.Trim());
            RDcard.WriteLine(m_txtFullNameT.Text.Trim());  
            RDcard.WriteLine(m_txtFullNameE.Text.Trim());
            RDcard.WriteLine(m_txtAddress.Text.Trim());
            RDcard.WriteLine(m_txtBrithDate.Text.Trim());
            RDcard.WriteLine(m_txtGender.Text.Trim());
            RDcard.WriteLine(m_txtIssueDate.Text.Trim());
            RDcard.WriteLine(m_txtExpiryDate.Text.Trim());
            RDcard.WriteLine(m_txtIssueNum.Text.Trim());
            // หมายเหตุ ถ้ามีใช้ไปด้วย 
            cTemp=m_TxtDetail_1.Text.Trim(); if (cTemp!="") {RDcard.WriteLine(cTemp);}
            cTemp=m_TxtDetail_2.Text.Trim(); if (cTemp!="") {RDcard.WriteLine(cTemp);}
            cTemp=m_TxtDetail_3.Text.Trim(); if (cTemp!="") {RDcard.WriteLine(cTemp);}
            cTemp=m_TxtDetail_4.Text.Trim(); if (cTemp!="") {RDcard.WriteLine(cTemp);}
            cTemp=m_TxtDetail_5.Text.Trim(); if (cTemp!="") {RDcard.WriteLine(cTemp);}
            cTemp=m_TxtDetail_6.Text.Trim(); if (cTemp!="") {RDcard.WriteLine(cTemp);}
            RDcard.Close();

            RDcard = new StreamWriter("lastRdcard.txt");
            RDcard.WriteLine(Util.Program.DTN(4)+ " ➽ " + m_txtFullNameT.Text.Trim());
            RDcard.Close();

            // เปลี่ยนภาษาไทย แปลงเป็น WindowCode เพื่อให้โปรแกรมดึงได้ RDcard.txt
            StreamReader sr = new StreamReader(cfFileText);
            StreamWriter sw = new StreamWriter(cfFileTextVFP, false, Encoding.Default);
            sw.WriteLine(sr.ReadToEnd());
            sw.Close();
            sr.Close();
            File.Copy(@"_Card\RDcardUTF8.txt",NIDCardCS.Program.Folder4IDcard+@"History\"+m_txtFullNameT.Text.Trim()+"_T.txt",true);
            File.Copy(@"_Card\RDcardUTF8.txt", NIDCardCS.Program.Folder4IDcard+"ล่าสุด_T.txt", true);

            // เก็บประวัติต่างๆ
            using (StreamWriter HistoryTxt = File.AppendText(NIDCardCS.Program.History4IDcard))
            {
                cTemp = Util.Program.DTN(4)+ "  🎫" + m_txtID.Text.Trim()+ " 👮" + m_txtFullNameT.Text.Trim();
                HistoryTxt.WriteLine(cTemp);
                cTemp = "";
                HistoryTxt.Close();HistoryTxt.Dispose();
            }
            Util.Program.ReverseTextFile(NIDCardCS.Program.History4IDcard, NIDCardCS.Program.History4IDcardR,50);
        }


        private void Save4FaceIMG() // Save เฉพาะส่วนหน้า 
        {
            m_picPhoto.Image.Save(@"_Card\Face.jpg", ImageFormat.Jpeg);
            File.Copy(@"_Card\Face.jpg", NIDCardCS.Program.Folder4IDcard +@"History\"+m_txtFullNameT.Text.Trim()+"_F.jpg",true);
            File.Copy(@"_Card\Face.jpg", NIDCardCS.Program.Folder4IDcard + "ล่าสุด_F.jpg", true);
        }


        private void Save4DeskTOP() // Save หน้าจอเป็นรูปภาพ 
        {
            /*
            Int32 xFirst = Form1.ActiveForm.DesktopLocation.X;
            Int32 yFirst = Form1.ActiveForm.DesktopLocation.Y;
            Int32 xLast =  Form1.ActiveForm.DesktopBounds.X;
            Int32 yLast =  Form1.ActiveForm.DesktopBounds.Y ;
            เฉพาะรูป 
            Bitmap printscreen = new Bitmap(Form1.ActiveForm.Height,Form1.ActiveForm.Width);
            // เต็มหน้าจอ 
            Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            */
                if (fwidth<=0) {fwidth=Screen.PrimaryScreen.Bounds.Width;}
            if (fheight<=0) {fheight=Screen.PrimaryScreen.Bounds.Height;}
            Bitmap printscreen = new Bitmap(fwidth,fheight);  /* กว้างXยาว ให้เปิดด้วย Paint Brash ปรับ แล้ว ดู ตัวเลข Properties แล้ว  */
            Graphics graphics = Graphics.FromImage(printscreen as Image);
            graphics.CopyFromScreen(xFirst, yFirst,xLast,yLast, printscreen.Size);
            printscreen.Save(@"_Card\Desktop.jpg", ImageFormat.Jpeg);
            printscreen.Dispose();
            Write2Jpeg(@"_Card\Desktop.jpg",2);
            File.Copy(@"_Card\Desktop.jpg", NIDCardCS.Program.Folder4IDcard +@"History\"+m_txtFullNameT.Text.Trim()+"_D.jpg",true);
            File.Copy(@"_Card\Desktop.jpg", NIDCardCS.Program.Folder4IDcard + "ล่าสุด_D.jpg", true);
        }



        public static void Write2Jpeg(string cFLSaveName,int nOption) // เขียน ข้อความ ในรูป
        {
            string cfCompany;
            cfCompany = "";
            if (File.Exists("RdCardInfo.txt"))
            {   string[] lines = File.ReadAllLines("RdCardInfo.txt");
                foreach (string line in lines) { cfCompany = cfCompany + line.Trim();    }
            }
            string fleInTemp;
            switch (nOption)
            {
                case 1: fleInTemp = @"Temp\tempJpg1.jpg";   break;
                case 2: fleInTemp = @"Temp\tempJpg2.jpg"; break;
                case 3: fleInTemp = @"Temp\tempJpg3.jpg"; break;
                default: fleInTemp = @"Temp\tempJpg.jpg"; break;
            }
            File.Copy(cFLSaveName,fleInTemp, true);
            var bmp = Bitmap.FromFile(fleInTemp);
            var newImage = new Bitmap(bmp.Width, bmp.Height);
            var gr = Graphics.FromImage(newImage);
            Font drawFont = new Font("Tomaha", 20);
            SolidBrush drawcolor ;
            gr.DrawImageUnscaled(bmp, 0, 0);
            string myText = "ใช้สำหรับ =" + cfCompany.Trim() + "= เท่านั้น";
            switch (nOption)
            {
                case 2: drawcolor = new SolidBrush(Color.Blue); break;
                case 3: drawcolor = new SolidBrush(Color.Red); myText = "-Error: " + myText; break;
                default: drawcolor = new SolidBrush(Color.White); break;
            }
            gr.DrawString(myText , drawFont, drawcolor , new RectangleF(10, 10, 0, 50));
            gr.DrawString("@วันที่ " + DateTime.Now.ToString("dd/MM/yyyy")+" " + DateTime.Now.ToString("HH:mm") + " น.", drawFont, drawcolor, new RectangleF(10, 40, 0, 50));
            newImage.Save(cFLSaveName);
            newImage.Dispose();
        }


        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Activated(object sender, EventArgs e)
        {

        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            if (NIDCardCS.Program.cpFlag=="2") { Ok2Save(); Application.Exit(); }  // เลือก 2 ออกจากโปรแกรมเลย 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter--;
            if (counter == 0)
            {
                Rd4Exit(2,""); 
                this.Close(); }
                cTimer.Text = counter.ToString();
            }

        private void nSample_CheckedChanged(object sender, EventArgs e)
        {
            if (nSample.Checked == true) { if (!File.Exists("RdcardDemo.txt")) { File.Copy("RdCardInfo.txt", "RdcardDemo.txt"); } }
            else { if (File.Exists("RdcardDemo.txt")) { File.Delete("RdcardDemo.txt"); } }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Rd4Exit(2);   
        }

        private void cTimer_Click(object sender, EventArgs e)
        {

        }
    }
}
