Example #1
0
 private void chkPicFromCam_CheckedChanged(object sender, EventArgs e)
 {
     if (chkPicFromCam.Checked)
     {
         capturer.Capture(0);
         System.Threading.Thread.Sleep(3000);
         CaptureSingleShotTimer.Enabled = true;
         CaptureSingleShotTimer.Start();
     }
     else
     {
         capturer.ReleaseCapture();
     }
 }
Example #2
0
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();

            if (chkPicFromCam.Checked)
            {
                CaptureSingleShotTimer.Stop();
                string strTemp = System.IO.Path.GetTempFileName().Replace(".tmp", ".bmp");
                int    x = -1, y = -1, w = -1, h = -1;

                capturer.NextFrame(strTemp, chkAcurateFaceDetection.Checked ? 1 : 0, out x, out y, out w, out h);

                if (w > 0 && h > 0)
                {
                    Image img = new Bitmap(strTemp);

                    Bitmap   newImg = new Bitmap(w, h);
                    Graphics gr     = Graphics.FromImage(newImg);
                    gr.DrawImage(img, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
                    string newPath = "Faces\\" + new Random().Next().ToString() + ".bmp";
                    img.Dispose();
                    img = null;
                    GC.Collect();
                    newImg.Save(newPath);
                    picFace.ImageLocation = newPath;

                    System.IO.File.Delete(strTemp.Replace(".bmp", ".tmp"));
                    System.IO.File.Delete(strTemp);
                }
                else
                {
                    MessageBox.Show("تصویر انتخاب شده فاقد چهره می باشد");
                }
            }
            else
            {
                of.Filter = "|*.jpeg;*.jpg;*.JPEG;*.JPG|*.bmp;*.BMP|*.png;.PNG|*.tiff;*.tif;*.TIFF;*.TIF|";
                if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (capturer == null)
                    {
                        capturer = new FaceLib.Capturer();
                    }
                    int x = -1, y = -1, w = -1, h = -1;


                    capturer.ExtractFace(of.FileName, chkAcurateFaceDetection.Checked ? 1 : 0, out x, out y, out w, out h);
                    if (w > 0 && h > 0)
                    {
                        Image img = new Bitmap(of.FileName);

                        Bitmap   newImg = new Bitmap(w, h);
                        Graphics gr     = Graphics.FromImage(newImg);
                        gr.DrawImage(img, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
                        string newPath = "Faces\\" + new Random().Next().ToString() + ".bmp";
                        newImg.Save(newPath);
                        picFace.ImageLocation = newPath;
                    }
                    else
                    {
                        MessageBox.Show("تصویر انتخاب شده فاقد چهره می باشد");
                    }
                }
            }
        }