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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM