public void ShowImage(TFaceRecord DetectFaceFace)
        {
            int iWidth = 587;
            int iHeight = 600;
            int iy = 150;
            Image img = DetectFaceFace.image.ToCLRImage();
            pictureBox1.Image = img;
            pictureBox1.Location = new Point(43, 208); //new Point(43, 208);//23, 397
            pictureBox1.Height = iHeight;
            pictureBox1.Width = 719;

            iy = iy + iHeight;

            this.Width = 800;
            this.Height = 1224;
            lblHeader.Location = new Point(306, iy + 50);
            lblLine1.Location = new Point(125, iy + 100);
            lblLine2.Location = new Point(125, iy + 150);
            lblLine3.Location = new Point(125, iy + 200);
            pictureOK.Location = new Point(210, iy + 370);

              //  lblLine1.Text ="คุณ" +  GetFileName(DetectFaceFace.ImageFileName);
            //pictureBox1.Height = img.Height;
            // pictureBox1.Width = img.Width;
            // this.Show();
            //  frmMain F = new frmMain();
               SetDetail(GetFileName(DetectFaceFace.ImageFileName));
        }
        public Boolean CheckFile()
        {
            Boolean ret;
            ret = true;
            string strpath2 = LiveFaceScan.CameraSetting.Drive + ":\\Kiosk_Image_Search\\imagecompare.jpg"; //"D:\\Kiosk_Image\\9999.jpg";
            strpath2 = strpath2.Replace("\\", "/");
            string fn = strpath2;//dlg.FileNames[0];
            TFaceRecord fr = new TFaceRecord();
            fr.ImageFileName = fn;
            fr.FacePosition = new FSDK.TFacePosition();
            fr.FacialFeatures = new FSDK.TPoint[FSDK.FSDK_FACIAL_FEATURE_COUNT];
            fr.Template = new byte[FSDK.TemplateSize];

            try
            {
                Image imgSerach = Image.FromFile(strpath2); //pictureBox1.Image;//
                fr.image = new FSDK.CImage(imgSerach);
                //fr.image = new FSDK.CImage(fn);

            }
            catch (Exception ex)
            {
                ret =  false;
            }

            fr.FacePosition = fr.image.DetectFace();
            if (0 != fr.FacePosition.w)
            //    1 = 1;
            //// MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
            //else
            {
                fr.faceImage = fr.image.CopyRect((int)(fr.FacePosition.xc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.xc + Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc + Math.Round(fr.FacePosition.w * 0.5)));

                bool eyesDetected = false;
                try
                {
                    fr.FacialFeatures = fr.image.DetectEyesInRegion(ref fr.FacePosition);
                    eyesDetected = true;
                }
                catch (Exception ex)
                {
                    ret = false;
                }

                if (eyesDetected)
                {
                    fr.Template = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition); // get template with higher precision
                }
            }
            else
            {
                // MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                //MessageBox.Show("กรุณาถ่ายใหม่ค่ะ");
                //needClose = false;
                //PopulateData();
                ret = false;
            }

            return ret;
        }
        public void PopulateAllImage()
        {
            try {
                int percents;
            FaceList = new List<TFaceRecord>();
            string strapppath = LiveFaceScan.CameraSetting.Drive + ":\\Kiosk_Image";// System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
               // strapppath = strapppath + "\\images";
            String[] filenames = System.IO.Directory.GetFiles(strapppath, "*.jpg");

            //imageList1 = new ImageList();
            //Size sizeCustom = new Size();
            //sizeCustom.Height = 50;
            //sizeCustom.Width = 50;
            //imageList1.ImageSize = sizeCustom;
            //imageList1.ColorDepth = ColorDepth.Depth24Bit;

            for (int i = 0; i < filenames.Length; ++i)
            {

                if (status == true)
                {
                    percents = (filenames.Length * 100) / filenames.Length;
                    bgw.ReportProgress(percents, filenames.Length-1);
                    return;
                }

                try
                {
                    string strpath = filenames[i].Replace("\\", "/");

                    Image img = Image.FromFile(strpath);
                TFaceRecord fr2 = new TFaceRecord();
                fr2.ImageFileName =strpath;
                fr2.FacePosition = new FSDK.TFacePosition();
                fr2.FacialFeatures = new FSDK.TPoint[2];
                fr2.Template = new byte[FSDK.TemplateSize];
                fr2.image = new FSDK.CImage(img);

                fr2.FacePosition = fr2.image.DetectFace();
                if (0 != fr2.FacePosition.w)
                //    if (strpath.Length <= 1)
                //        MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                //    else
                //        strpath = strpath;   //this.txtDetect.Text += (fn + ": No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.\r\n");
                //else
                {
                    fr2.faceImage = fr2.image.CopyRect((int)(fr2.FacePosition.xc - Math.Round(fr2.FacePosition.w * 0.5)), (int)(fr2.FacePosition.yc - Math.Round(fr2.FacePosition.w * 0.5)), (int)(fr2.FacePosition.xc + Math.Round(fr2.FacePosition.w * 0.5)), (int)(fr2.FacePosition.yc + Math.Round(fr2.FacePosition.w * 0.5)));

                    try
                    {
                        fr2.FacialFeatures = fr2.image.DetectEyesInRegion(ref fr2.FacePosition);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "Error detecting eyes.");
                    }

                    try
                    {
                        fr2.Template = fr2.image.GetFaceTemplateInRegion(ref fr2.FacePosition); // get template with higher precision
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "Error retrieving face template.");
                    }

                    FaceList.Add(fr2);

                    string strpath2 = LiveFaceScan.CameraSetting.Drive + ":\\Kiosk_Image_Search\\imagecompare.jpg"; //"D:\\Kiosk_Image\\9999.jpg";
                    strpath2 = strpath2.Replace("\\", "/");
                    string fn = strpath2;//dlg.FileNames[0];
                    TFaceRecord fr = new TFaceRecord();
                    fr.ImageFileName = fn;
                    fr.FacePosition = new FSDK.TFacePosition();
                    fr.FacialFeatures = new FSDK.TPoint[FSDK.FSDK_FACIAL_FEATURE_COUNT];
                    fr.Template = new byte[FSDK.TemplateSize];

                    try
                    {
                        Image imgSerach = Image.FromFile(strpath2); //pictureBox1.Image;//
                        fr.image = new FSDK.CImage(imgSerach);
                        //fr.image = new FSDK.CImage(fn);

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error loading file");
                    }

                    fr.FacePosition = fr.image.DetectFace();
                    if (0 != fr.FacePosition.w)
                    //    1 = 1;
                    //// MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                    //else
                    {
                        fr.faceImage = fr.image.CopyRect((int)(fr.FacePosition.xc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.xc + Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc + Math.Round(fr.FacePosition.w * 0.5)));

                        bool eyesDetected = false;
                        try
                        {
                            fr.FacialFeatures = fr.image.DetectEyesInRegion(ref fr.FacePosition);
                            eyesDetected = true;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error detecting eyes.");
                        }

                        if (eyesDetected)
                        {
                            fr.Template = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition); // get template with higher precision
                        }
                    }
                    else
                    {
                       // MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                        //MessageBox.Show("กรุณาถ่ายใหม่ค่ะ");
                        //needClose = false;
                        //PopulateData();
                        //return;
                    }

                    float Threshold = 0.0f;
                    FSDK.GetMatchingThresholdAtFAR(frmCameraDetect.FARValue / 100, ref Threshold);
                    int MatchedCount = 0;
                    //int FaceCount = frmCameraDetect.FaceList.Count; //frmCameraDetect.FaceList.Count;
                    //float[] Similarities = new float[FaceCount];
                    //int[] Numbers = new int[FaceCount];
                    float Similarities = 0.0f;
                    float Similarity = 0.0f;
                   // TFaceRecord CurrentFace = FaceList[i];
                    FSDK.MatchFaces(ref fr.Template, ref fr2.Template, ref Similarity);
                    if (Similarity >= Threshold)
                    {
                        Similarities = Similarity;
                        ++MatchedCount;
                    }
                    int icount;
                    icount = i + 1;

                    if (icount == 128) {

                      //  lblPercent.Text = "รุปที่ " + icount + " ความเหมือน=" + (Similarities * 100) + "%";
                    }

                  //  lblPercent.Text = "รุปที่ " + icount + " ความเหมือน=" + (Similarities * 100) + "%";
                    if (Similarities * 100 >= 85)
                    {

                        frCompare = fr2;

                        //FSDKCam.CloseVideoCamera(0);
                        needClose = true;
                        //// this.Close();

                        //this.Visible = false;
                        //this.Close();
                        //frmCameraResult Detect = new frmCameraResult();
                        //Detect.StartPosition = FormStartPosition.CenterScreen;
                        //Detect.ShowImage(fr2);
                        //Detect.ShowDialog();
                        //MessageBox.Show(Similarities * 100 + "");
                        return;

                    }
                    //imageList1.Images.Add(fr2.faceImage.ToCLRImage());
                    //lvRegister.Items.Add((imageList1.Images.Count - 1).ToString(), strpath, imageList1.Images.Count - 1);
                }
                }
                catch (Exception exfor)
                {

                }

                //System.Threading.Thread.Sleep(100);
                percents = (i * 100) / filenames.Length;
                bgw.ReportProgress(percents, i);

            }

            //lvRegister.OwnerDraw = false;
            //lvRegister.View = View.LargeIcon;
            //lvRegister.Dock = DockStyle.Right;
            //lvRegister.LargeImageList = imageList1;

            }
            catch (Exception exPopulateAllImage)
            {

            }
        }
        public void ImageDetect()
        {
            if (frmCameraDetect.FaceList.Count == 0)
                MessageBox.Show("Please enroll faces first", "Error");
            else
            {
               // string strapppath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\imagesearch\\imagecompare.jpg";
                string strapppath = LiveFaceScan.CameraSetting.Drive + ":\\Kiosk_Image_Search\\imagecompare.jpg";
                strapppath = strapppath.Replace("\\", "/");
                string fn = strapppath;//dlg.FileNames[0];
                TFaceRecord fr = new TFaceRecord();
                fr.ImageFileName = fn;
                fr.FacePosition = new FSDK.TFacePosition();
                fr.FacialFeatures = new FSDK.TPoint[FSDK.FSDK_FACIAL_FEATURE_COUNT];
                fr.Template = new byte[FSDK.TemplateSize];

                try
                {
                    fr.image = new FSDK.CImage(fn);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error loading file");
                }

                fr.FacePosition = fr.image.DetectFace();
                if (0 != fr.FacePosition.w)
                //    1 = 1;
                //// MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                //else
                {
                    fr.faceImage = fr.image.CopyRect((int)(fr.FacePosition.xc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.xc + Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc + Math.Round(fr.FacePosition.w * 0.5)));

                    bool eyesDetected = false;
                    try
                    {
                        fr.FacialFeatures = fr.image.DetectEyesInRegion(ref fr.FacePosition);
                        eyesDetected = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error detecting eyes.");
                    }

                    if (eyesDetected)
                    {
                        fr.Template = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition); // get template with higher precision
                    }
                }
                else
                {
                    //MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                }

                float Threshold = 0.0f;
                FSDK.GetMatchingThresholdAtFAR(frmCameraDetect.FARValue / 100, ref Threshold);
                int MatchedCount = 0;
                int FaceCount = frmCameraDetect.FaceList.Count; //frmCameraDetect.FaceList.Count;
                float[] Similarities = new float[FaceCount];
                int[] Numbers = new int[FaceCount];
                for (int i = 0; i < frmCameraDetect.FaceList.Count; i++)
                {
                    float Similarity = 0.0f;
                    TFaceRecord CurrentFace = frmCameraDetect.FaceList[i];
                    FSDK.MatchFaces(ref fr.Template, ref CurrentFace.Template, ref Similarity);
                    if (Similarity >= Threshold)
                    {
                        Similarities[MatchedCount] = Similarity;
                        Numbers[MatchedCount] = i;
                        ++MatchedCount;
                    }

                    if (Similarities[i] * 100 >= 90)
                    {

                        FSDKCam.CloseVideoCamera(0);
                        needClose = true;
                        // this.Close();

                        this.Visible = false;
                        this.Close();
                        frmCameraResult Detect = new frmCameraResult();
                        Detect.StartPosition = FormStartPosition.CenterScreen;
                        Detect.ShowImage(CurrentFace);
                        Detect.ShowDialog();
                        return;

                    }
                }

            }
        }