public void showCamera(PictureBox picBox) { while (!needClose) { Int32 imageHandle = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current frame from the camera { Application.DoEvents(); continue; } IntPtr hbitmapHandle = IntPtr.Zero; // to store the HBITMAP handle FSDK.SaveImageToHBitmap(imageHandle, ref hbitmapHandle); Image frameImage = Image.FromHbitmap(hbitmapHandle); Graphics gr = Graphics.FromImage(frameImage); string[] strResult = new string[2]; Pen xPen = new Pen(Color.Red, 5); // if a face is detected, we can recognize it if (FSDK.FSDKE_OK == FSDK.DetectFace(imageHandle, ref facePosition)) { gr.DrawRectangle(xPen, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2, facePosition.w, facePosition.w); //gr.DrawEllipse(xPen, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2, facePosition.w, facePosition.w); // create a new face template byte[] template = new byte[FSDK.TemplateSize]; FSDK.GetFaceTemplateInRegion(imageHandle, ref facePosition, out template); strResult = MatchTemplateWithDatabase(template); // if face matches with database's face data if (!strResult[0].Equals("")) { xVoice = new SpeechSynthesizer(); xVoice.Speak("Hello " + strResult[1]); //speak by name xVoice.Dispose(); } } else // if there is no face in the screen, then background music will be played { objBkMusic.playMusic(); // doesn't work
} picBox.Image = frameImage; FSDK.FreeImage(imageHandle); // delete the FSDK image handle DeleteObject(hbitmapHandle); // delete the HBITMAP object GC.Collect(); // collect the garbage after the deletion // make UI controls accessible Application.DoEvents(); } FSDKCam.CloseVideoCamera(cameraHandle); FSDKCam.FinalizeCapturing(); }
}