• WPF中使用WPFMediaKit视频截图案例


    前台 代码:

    <Window x:Class="WpfAppWPFMediaKit.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfAppWPFMediaKit"
    xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
    <DockPanel>
    <DockPanel DockPanel.Dock="Top">
    <ComboBox Name="cb" DockPanel.Dock="Top" SelectionChanged="cb_SelectionChanged"></ComboBox>
    <Button Click="Button_Click" DockPanel.Dock="top" Height="45" Content="拍照" Margin="160,0,92,0"/>
    </DockPanel>
    <wpfmedia:VideoCaptureElement Name="vce" ></wpfmedia:VideoCaptureElement>
    </DockPanel>
    </Grid>
    </Window>

    后台代码:

    using System.IO;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using WPFMediaKit.DirectShow.Controls;

    namespace WpfAppWPFMediaKit
    {
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    //现在电脑上装的所有摄像头中,选择一个摄像头。
    cb.ItemsSource = MultimediaUtil.VideoInputNames;
    //设置第0个摄像头为默认摄像头。
    if (MultimediaUtil.VideoInputNames.Length > 0)
    {
    cb.SelectedIndex = 0;
    }
    else
    {
    MessageBox.Show("电脑没有安装任何摄像头");
    }
    }

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
    //抓取控件做成图片
    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);
    byte[] captureData = ms.ToArray();
    //保存图片
    File.WriteAllBytes("E:/1.jpg", captureData);
    }
    vce.Pause();
    }
    }
    }

    项目使用的类库:WPFMediaKit.dll 和 DirectShowLib-2005.dll

  • 相关阅读:
    二叉树
    基础1
    tcp/udp
    异步io模块(自定义)
    select+异步
    异步发送请求的几种方式
    多线程,进程实现并发请求
    位解包运算
    从前端程序员的视角看小程序的稳定性保障
    运行node 报错 throw er; // Unhandled 'error' event
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/7998049.html
Copyright © 2020-2023  润新知