wpf中怎么调用surface(或者其他平板)的摄像头进行拍照那?今天分享一下使用WPFMediaKit调用平板(surface2)摄像头拍照。
下载最新的代码,加入到项目中。
2、test项目引用WPFMediaKit
xaml代码:
代码如下 | 复制代码 |
<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="100" /> <Button Content="打开摄像头" x:Name="btnOpen" Click="btnOpen_Click"Width="100"/> <Button Content="拍照" x:Name="btnCapture" Click="btnCapture_Click"Width="100" /> <Button Content="保存" x:Name="btnSave" Click="btnSave_Click" Width="100" /> </StackPanel> <wpfmedia:VideoCaptureElement Name="vce" /> </StackPanel> </Grid> </Window> |
后台代码:
获取摄像头:
代码如下 | 复制代码 |
private void cameraOpen() { cb.ItemsSource = MultimediaUtil.VideoInputNames; if (MultimediaUtil.VideoInputNames.Length > 1) { cb.SelectedIndex = 1; } else { cb.SelectedIndex = 0; } } |
拍照保存到硬盘:win8注意有文件夹的权限问题
代码如下 | 复制代码 |
private void TakePhoto() { RenderTargetBitmap bmp = new RenderTargetBitmap( (int)vce.ActualWidth, (int)vce.ActualHeight, 96, 96, PixelFormats.Default); //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; } |
时间: 2024-12-01 16:50:20