WPF调用摄像头(调用WPFMediaKit库)

一、引用-(右键)管理NuGet程序包或菜单-项目-管理NuGet程序包WPF调用摄像头(调用WPFMediaKit库)_第1张图片
二、浏览-搜索WPFMediaKit并安装-安装成功后引用中已自动添加了mediakit
WPF调用摄像头(调用WPFMediaKit库)_第2张图片
三、添加代码
xaml代码中加入:

xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"

以及Grid中

<WPFMediaKit:VideoCaptureElement x:Name="m_VideoPlayer" Height="1080" Width="1920" Grid.Row="1"></WPFMediaKit:VideoCaptureElement>

完整如下:

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
        Title="MainWindow" Height="1080" Width="1920">
    <Grid>
        <StackPanel>
            <WPFMediaKit:VideoCaptureElement x:Name="VideoPlayer" Height="1080" Width="1920" Grid.Row="1"></WPFMediaKit:VideoCaptureElement>
        </StackPanel>
        <Image HorizontalAlignment="Left" Height="1080" Margin="-2,-31,-6,0" VerticalAlignment="Top" Width="1920" Source="p1.png"/>

    </Grid>
</Window>

MainWindow.xaml.cs代码:using WPFMediaKit.DirectShow.Controls;

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
            string[] inputNames = MultimediaUtil.VideoInputNames;//获取摄像头
            foreach (string ss in inputNames)
            {
                VideoPlayer.VideoCaptureSource = ss;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            VideoPlayer.Pause();
        }   
        
    }

四、AForge库也可以调用摄像头,但它只能在界面的最上层,而MediaKit可以在其上覆盖控件

你可能感兴趣的:(项目学习)