Add assembly: WPFMediaKit.dll
The more critical code is as follows:
The interface design code is as follows:
<Windowx:class= "camera called." MainWindow "xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= "All"Width= "$"Xmlns:wpfmedia= "Clr-namespace:wpfmediakit.directshow.controls;assembly=wpfmediakit"windowstartuplocation= "Centerscreen" > <Grid> <StackPanelOrientation= "Vertical"> <StackPanelOrientation= "Horizontal"> <ComboBoxName= "CB"selectionchanged= "Cb_selectionchanged"Width= "+" ></ComboBox> <ButtonWidth= "a"Content= "Pat"Name= "Btncapture"Click= "Btncapture_click"Margin= "200,0,0,0"></Button> <ButtonWidth= "a"Content= "Re-pat"Name= "Btnrestart"Click= "Restart_click"></Button> </StackPanel> <wpfmedia:videocaptureelementHeight= "434"x:name= "VCE"Stretch= "Fill"Margin= "172,30,185,0"Rendertransformorigin= "0.5,0.5" > <Wpfmedia:VideoCaptureElement.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransformAngle= " -90"/> <TranslateTransform/> </TransformGroup> </Wpfmedia:VideoCaptureElement.RenderTransform> </wpfmedia:videocaptureelement> </StackPanel> </Grid></Window>
Handling Event Codes
usingSystem;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.IO;usingWPFMediaKit.DirectShow.Controls;namespaceCamera Call {/// <summary> ///the interactive logic of MainWindow.xaml/// </summary> Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); Cb. ItemsSource= Multimediautil.videoinputnames;//Get all Cameras if(MultimediaUtil.VideoInputNames.Length >0) {cb. SelectedIndex=0;//No. 0 Camera for default camera } Else{MessageBox.Show ("The computer does not have any available cameras installed"); } } Private voidBtncapture_click (Objectsender, RoutedEventArgs e)//Take Pictures{rendertargetbitmap bmp=NewRenderTargetBitmap ((int) VCE. ActualWidth, (int) VCE. ActualHeight,//VCE is the name of the foreground Wpfmedia control the, the, Pixelformats.default); //To avoid scratching, you need to call measure, Arrange before render//in order to avoid videocaptureelement display, it is necessary to//Videocaptureelement's stretch= "Fill"VCE. Measure (VCE. Rendersize); Vce. Arrange (NewRect (VCE. Rendersize)); Bmp. Render (VCE); Bitmapencoder Encoder=NewJpegbitmapencoder (); Encoder. Frames.add (Bitmapframe.create (BMP)); using(MemoryStream ms =NewMemoryStream ()) {Encoder. Save (MS); byte[] Capturedata =Ms. ToArray (); File.writeallbytes (@"./photo/"+ Guid.NewGuid (). ToString (). Substring (0,5) +". jpg", Capturedata); } VCE. Pause (); } //re-shoot Private voidRestart_click (Objectsender, RoutedEventArgs e) {VCE. Play (); } Private voidCb_selectionchanged (Objectsender, SelectionChangedEventArgs e)//Selection events for ComboBox Controls{VCE. Videocapturesource= (string) CB. SelectedItem;//VCE is the name of the foreground Wpfmedia control } }}
WPF calls the camera