Using the Aforge.net control camera in WPF to take pictures

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.