本文介绍了如何在WPF中捕获视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小型应用程序,可以从网络摄像头捕获视频并将其保存在我的计算机中.我已经完成了一些代码,但是没有检测到网络摄像头.非常感谢.
我将我的代码放在这里.
Window1.xaml

i am developing a small application which capture video from web cam and save in my computer location.i have done some code but its not detecting the web cam.plz help me. thanks in advanced.
i am putting my code here.
Window1.xaml

<Window x:Class="AvCapWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:AvCapWPF"
    Name="wnd"
    Title="Window1" Height="291" Width="286">
    <Window.Resources>
        <l:ScaleConverter x:Key="conv"/>
    </Window.Resources>
        <Grid>
        <l:CapPlayer x:Name="player" RenderTransformOrigin="0.5,0.5">
            <l:CapPlayer.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="1" ScaleY="1"/>
                    <SkewTransform AngleX="0" AngleY="0"/>
                    <RotateTransform Angle="180.294"/>
                    <TranslateTransform X="0" Y="0"/>
                </TransformGroup>
            </l:CapPlayer.RenderTransform>
        </l:CapPlayer>
        <HeaderedItemsControl Header="Framerate" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="55">
            <Grid>
                <Rectangle Fill="#55FF0000">
                    <Rectangle.Height>
                        <MultiBinding Converter="{StaticResource conv}">
                            <Binding ElementName="player" Path="Framerate"/>
                            <Binding ElementName="wnd" Path="ActualHeight"/>
                        </MultiBinding>
                    </Rectangle.Height>
                </Rectangle>
                <TextBlock Text="{Binding ElementName=player, Path=Framerate}" HorizontalAlignment="Left" Width="13" />
            </Grid>
        </HeaderedItemsControl>
        <Button Height="23" Name="btnStart" VerticalAlignment="Bottom" Margin="0,0,74,0" Click="btnStart_Click" HorizontalAlignment="Right" Width="61">Start</Button>
        <Button Height="23" Margin="0,0,8,0" Name="button2" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="61">Stop</Button>
        <Button Height="23" Margin="74,0,0,0" Name="btnCue" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="48" Click="btnCue_Click">cue</Button>
    </Grid>
</Window


>



window1.xaml.cs


>



window1.xaml.cs

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.Navigation;
using System.Windows.Shapes;
using DirectX.Capture;
namespace AvCapWPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private Capture capture = null;
        public Window1()
        {
            InitializeComponent();

        }
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //capture.PreviewWindow = player;
                //capture.VideoSource =
               // capture.VideoDevice;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to enable/disable preview. Please submit a bug report.\n\n" + ex.Message + "\n\n" + ex.ToString());
            }

            try
            {
                if (capture == null)
                    throw new ApplicationException("Please select a video and/or audio device.");
                if (!capture.Cued)
                    capture.Filename = "D:\anay.avi";
                capture.Start();
                btnCue.IsEnabled = false;
                btnStart.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.ToString());
            }
        }
        private void btnCue_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (capture == null)
                    throw new ApplicationException("Please select a video and/or audio device.");
                if (!capture.Cued)
                    capture.Filename = "D:\anay.avi";
                capture.Cue();
                btnCue.IsEnabled = false;
                MessageBox.Show("Ready to capture.\n\nUse Cue() before Start() to " +
                    "do all the preparation work that needs to be done to start a " +
                    "capture. Now, when you click Start the capture will begin faster " +
                    "than if you had just clicked Start. Using Cue() is completely " +
                    "optional. The downside to using Cue() is the preview is disabled until " +
                    "the capture begins.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.ToString());
            }
        }
    }
    public class ScaleConverter : IMultiValueConverter
    {

        #region IMultiValueConverter Members
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float v = (float)values[0];
            double m = (double)values[1];
            return v * m / 50;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}

推荐答案


这篇关于如何在WPF中捕获视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:09