Today, we have a personnel project that calls the camera to take pictures of the employees in the project. How to solve this problem?
Introduce a development package to you, Mediakit. Everyone in the forum said yes, but the black rabbit thought everyone is good is really good. You might as well try it ~
Step One: Add the WPFMediaKit.dll file to the project
Step Two: Refer the WPFMediaKit.dll file in.
Step Right-click Reference, add Reference, browse tab, select the location where the WPFMediaKit.dll file is located.
Step Three: Add the following code to the top of the window (note that you don't want to remember it) just like using a class sample.
xmlns:wpfmediakit= "Clr-namespace:wpfmediakit.directshow.controls;assembly=wpfmediakit"
<window x:class= "Ixiahe.photos" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation " xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" title= "Photos" height= "+" width= "xmlns" : wpfmediakit= "Clr-namespace:wpfmediakit.directshow.controls;assembly=wpfmediakit" WindowStartupLocation= " Centerscreen "loaded=" Loaded ">
Fourth step: Add the Videocaptureelement control to display a preview of the screen (requires handwriting, because the toolbox does not have this control)
<StackPanel> <combobox name= "CB" selectionchanged= "cb_selectionchanged"/> <!--Select Camera -- <wpfmediakit:videocaptureelement height= "$" name= "VCE"/> <!--preview Screen- <button height= "x": Name= "Btncapture" content= "Take photos" click= "Btncapture_click"/> <!--camera button- </StackPanel>
Fifth step: Next is the background code
private void Window_Loaded (object sender, RoutedEventArgs e) {cb. ItemsSource = Multimediautil.videoinputnames; if (MultimediaUtil.VideoInputNames.Length > 0) {cb. SelectedIndex = 0;//No. 0 camera is default camera} else {MessageBox.Show ("No available camera installed on PC") ; }} private void Cbcameras_selectionchanged (object sender, SelectionChangedEventArgs e) { Captureelement.videocapturesource = (string) Cbcameras.selecteditem; }///<summary>///Photo///</summary> private void Btncapture_click (object sender, RoutedEventArgs e) {//captureelement. How to crawl the original image of HD rendertargetbitmap bmp = new R Endertargetbitmap ((int) captureelement.actualwidth, (int) captureelement.actualheight, (Pixelformats.default); To avoid scratching, you need to call MEAs before renderUre, Arrange//To avoid videocaptureelement display, need to//videocaptureelement stretch= "Fill" capture Element.measure (captureelement.rendersize); Captureelement.arrange (New Rect (captureelement.rendersize)); Bmp. Render (captureelement); Here you need to create a stream to store the pictures taken by the camera.
Of course, you can make the file stream, or you can make the memory flow.
Bitmapencoder encoder = new Jpegbitmapencoder (); Encoder. Frames.add (Bitmapframe.create (BMP)); Encoder. Save (MS); Captureelement.pause (); } <summary> ///re-pat///</summary> private void Btnanew_click (object sender, RoutedEventArgs e) { captureelement.play (); } <summary> ///Confirm///</summary> private void Btnok_click (object sender, RoutedEventArgs e) { capturedata = Ms. ToArray (); Ms. Dispose (); DialogResult = true; }
"Mediakit" WPF Project a development package that calls camera photography