WPF 摄像头拍照技术

第一步:添加WPFMediaKit.dll 文件到项目中

第二步:把WPFMediaKit.dll文件引用进来。

  步骤 右击引用—>添加引用—>浏览选项卡—>选择WPFMediaKit.dll文件所在的位置.

第三步:在窗口顶端加入如下代码(注意不要该意记)就像using一个类样。

  xmlns:wpfmedia="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="300" Width="300"
       xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
        WindowStartupLocation="CenterScreen" Loaded="loaded">

第四步:添加VideoCaptureElement控件 用来显示一个预览的画面 (需要手写,因为工具箱没有这个控件)

 <StackPanel>
            <ComboBox Name="cb" SelectionChanged="cb_SelectionChanged"/> <!--选摄像头-->
            <WPFMediaKit:VideoCaptureElement Height="200" Name="vce"/> <!--预览画面-->
            <Button Height="50" x:Name="btnCapture" Content="拍照" Click="btnCapture_Click"/> <!--拍照按钮-->
        </StackPanel>  

  接下来就是后台代码

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cbCameras.ItemsSource = MultimediaUtil.VideoInputNames;
            if (MultimediaUtil.VideoInputNames.Length > 0)
            {
                cbCameras.SelectedIndex = 0;//第0个摄像头为默认摄像头
            }
            else
            {
                MessageBox.Show("电脑没有安装任何可用摄像头");
            }
        }


        private void cbCameras_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            captureElement.VideoCaptureSource = (string)cbCameras.SelectedItem;
        }

        /// <summary>
        /// 拍照
        /// </summary>

        private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            //captureElement. 怎么抓取高清的原始图像           
            RenderTargetBitmap bmp = new RenderTargetBitmap(
                (int)captureElement.ActualWidth,
                (int)captureElement.ActualHeight,
                96, 96, PixelFormats.Default);

            //为避免抓不全的情况,需要在Render之前调用Measure、Arrange
            //为避免VideoCaptureElement显示不全,需要把
            //VideoCaptureElement的Stretch="Fill"
            captureElement.Measure(captureElement.RenderSize);
            captureElement.Arrange(new Rect(captureElement.RenderSize));
            bmp.Render(captureElement);

            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            encoder.Save(ms);
            captureElement.Pause();
        }

        /// <summary>
        /// 重拍
        /// </summary>

        private void btnanew_Click(object sender, RoutedEventArgs e)
        {
            captureElement.Play();
        }

        /// <summary>
        /// 确定
        /// </summary>

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            CaptureData = ms.ToArray();
            ms.Dispose();
            DialogResult = true;
        }
    
原文地址:https://www.cnblogs.com/qintangtao/p/2984066.html