Original: Use Aforge.net control camera to take pictures in WPF
The most convenient way to take a photo with the Aforge.net control camera is to use PictureBox to display the camera screen, but you cannot use PictureBox directly in WPF. The Exchange function must be provided through <WindowsFormsHost></WindowsFormsHost>. The workaround is as follows:
1. Create a new WPF application according to the usual method;
2. Adding references
Windowsformsintegration (support for interacting with WinForm)
System.Windows.Forms (WinForm control support)
Aforge.video and AForge.Video.DirectShow (copy aforge.video.dll,aforge.video.directshow.dll, camera Operation Library)
3. Add xmlns:wf= "Clr-namespace:system.windows.forms;assembly=system.windows.forms" in XAML ( Use <wf:PictureBox/> add PictureBox controls with WF instead of System.Windows.Forms
4, add in the corresponding location of the interface
<windowsformshost name= "WinForm" >
<wf:picturebox name= "Mypicture"/>
</WindowsFormsHost> (at this point, the setup of the interface layer is complete)
5. Code part
First initialize the camera when the window is loaded
Myphoto = Picturehost.child as System.Windows.Forms.PictureBox;
Filterinfocollection videodevices = new Filterinfocollection (filtercategory.videoinputdevice);
if (videodevices.count <= 0)
{
System.Windows.MessageBox.Show ("Please connect the camera");
Return
}
Else
{
Closecapturedevice ();
Mycapturedevice = new Videocapturedevice (Videodevices[0]. monikerstring); The//mycapturedevice type is Videocapturedevice,
Mycapturedevice.newframe + = new Newframeeventhandler (mycapturedevice_newframe);
Mycapturedevice.desiredframesize = new System.Drawing.Size (436, 360);//436, 360
Mycapturedevice.desiredframerate = 10;
Mycapturedevice.start ();
}
PictureBox Myphoto = Picturehost.child as system.windows.forms.picturebox;//gets the Mypicture control in the interface
void Mycapturedevice_newframe (object sender, Newframeeventargs EventArgs)//Frame Handler
{
Bitmap Bitmap = (Bitmap) eventArgs.Frame.Clone ();
Myphoto.image = Bitmap. Clone (
New RectangleF (bitmap. size.width-295)/2, (bitmap. size.height-413)/2, 295, 413),//display image with a width of 295 pixels and a height of 413 pixels
SYSTEM.DRAWING.IMAGING.PIXELFORMAT.FORMAT32BPPRGB);
}
Turn off the camera and release the system resources (must be called when the window is launched)
private void Closecapturedevice ()
{
if (mycapturedevice! = null)
{
if (mycapturedevice.isrunning)
{
Mycapturedevice.signaltostop ();
}
Mycapturedevice = null;
}
}
At this point, using the Aforge.net operating camera is basically done. The image captured by the camera can be displayed in the PictureBox, and if you want to take a picture just use Mycapturedevice.stop () to stop the camera, save the Image property in the PictureBox.
Originally wanted to display the camera directly using the image control in WPF, but always prompted in the frame handler that the frame image could not be manipulated, prompting: there is no permission action, and the other process owns the object (presumably this means). In this issue I tangled up about 10 days, has not found a solution, but also please expert guidance, thank you!!
Using the Aforge.net control camera in WPF to take pictures