UI

<Window x:Class="WpfApplication.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" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <ComboBox Name="cb" SelectionChanged="cb_SelectionChanged" />
            <wpfmedia:VideoCaptureElement Name="vce" Height="270" />
            <Button Content="拍照" x:Name="btnCapture" Click="btnCapture_Click" />
            <Button Content="保存" x:Name="btnSave" Click="btnSave_Click" />
        </StackPanel>
    </Grid>
</Window>

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFMediaKit.DirectShow.Controls;
using System.IO;

namespace WpfApplication
{
    /// <summary>
    /// WPFMediaKitWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WPFMediaKitWindow : Window
    {
        MemoryStream ms = null;
        public byte[] CaptureData { get; set; }

        public WPFMediaKitWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cb.ItemsSource = MultimediaUtil.VideoInputNames;
            if(MultimediaUtil.VideoInputNames.Length>0)
            {
                cb.SelectedIndex = 0;
            }
            else
            {

            }
        }

        private void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            vce.VideoCaptureSource = (string)cb.SelectedItem;
        }

        private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            //captureElement. 怎么抓取高清的原始图像
            //能不能抓视频。
            //todo:怎么只抓取一部分
            RenderTargetBitmap bmp = new RenderTargetBitmap(
                (int)vce.ActualWidth, (int)vce.ActualHeight,
                96, 96, PixelFormats.Default);
            bmp.Render(vce);
            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                CaptureData = ms.ToArray();
            }
            vce.Pause();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            using (FileStream fs = new FileStream(@"E:\changweihua\" + DateTime.Now.ToString("yyyy-mm-dd-hh-mm-sss")+".png", FileMode.OpenOrCreate))
            {
                //using (MemoryStream ms = new MemoryStream(CaptureData))
                //{
                //    ms.WriteTo(fs);
                //}
                fs.Write(CaptureData, 0, CaptureData.Length);
            }
        }
    }
}

知识共享许可协议
《WPF 使用 WPFMediaKit 实现拍照》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告

A PHP Error was encountered

Severity: Core Warning

Message: PHP Startup: zip: Unable to initialize module Module compiled with module API=20060613 PHP compiled with module API=20090626 These options need to match

Filename: Unknown

Line Number: 0

Backtrace: