How does WPF call surface (or other tablet) cameras to take pictures? Share today by using the Wpfmediakit call tablet (SURFACE2) camera to take photos.
Download the latest code and add it to the project.
2. Test Project Reference Wpfmediakit
XAML Code:
The code is as follows |
Copy Code |
<window x:class= "test. Wpfmediakitwindow " Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" Xmlns:wpfmedia= "Clr-namespace:wpfmediakit.directshow.controls;assembly=wpfmediakit" Title= "Wpfmediakitwindow" loaded= "window_loaded" windowstate= "maximized" > <Grid> <stackpanel orientation= "Vertical" > <stackpanel orientation= "Horizontal" > <combobox name= "CB" selectionchanged= "cb_selectionchanged" width= " <button content= "Open the Camera" x:name= "Btnopen" click= "Btnopen_click" width= "the/>" <button content= "photographed" x:name= "Btncapture" click= "Btncapture_click" width= "the/>" <button content= "Save" x:name= "Btnsave" click= "Btnsave_click" width= "the/>" </StackPanel> <wpfmedia:videocaptureelement name= "VCE"/>
</StackPanel> </Grid> </Window> |
Background code:
Get the camera:
The code is as follows |
Copy Code |
private void Cameraopen () { Cb. ItemsSource = Multimediautil.videoinputnames; if (MultimediaUtil.VideoInputNames.Length > 1) { Cb. SelectedIndex = 1; } Else { Cb. SelectedIndex = 0; } }
|
Photo Save to Hard disk: Win8 Note permissions issues with folders
The code is as follows |
Copy Code |
Private void Takephoto () { RenderTargetBitmap bmp = new RenderTargetBitmap ( (int) VCE. ActualWidth, (int) VCE. ActualHeight, , Pixelformats.default, etc.; //vce. Stretch = Stretch.fill; VCE. Measure (VCE. Rendersize); VCE. Arrange (new Rect (VCE). Rendersize)); BMP. Render (VCE); Bitmapencoder encoder = new Jpegbitmapencoder (); Encoder. Frames.add (Bitmapframe.create (BMP)); using (MemoryStream ms = new MemoryStream ()) { Encoder. Save (MS); Capturedata = Ms. ToArray (); File.writeallbytes (@ "c:aaaa" + DateTime.Now.ToString ("yyyy-mm-dd-hh-mm-sss") + ". png", capturedata); } VCE. Play (); //capcreatecapturewindowa () return; } |