zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    VS2010 自动跳过代码现象
    Reverse Linked List II 【纠结逆序!!!】
    Intersection of Two Linked Lists
    Linked List Cycle II
    Remove Nth Node From End of List 【另一个技巧,指针的指针】
    Swap Nodes in Pairs
    Merge Two Sorted Lists
    Remove Duplicates from Sorted List
    Linked List Cycle
    Dungeon Game
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/7998049.html
Copyright © 2011-2022 走看看