Webcam Grabber Sample Code

Dec 25, 2011 at 9:26 PM

Hi,

could anyone give me some sample code (C# if possible) to grab a webcam using WPF MediaKit?

Thanks a lot in advance,

MuckiSG

Jun 19, 2012 at 3:08 PM

hi

here is a sample for a tablet PC:  with front / rear camera

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
        Title="MainWindow" Height="465" Width="735" Loaded="Window_Loaded" mc:Ignorable="d" WindowState="Maximized" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" ResizeMode="CanResizeWithGrip" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MaxWidth="735" MaxHeight="465">
    <Grid Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

        <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,38,159,0" Width="auto" Height="auto">
            <WPFMediaKit:VideoCaptureElement  Stretch="Uniform"
                DesiredPixelWidth="1280" DesiredPixelHeight="1024" LoadedBehavior="Play" EnableSampleGrabbing="true"  
                FPS="15" Width="Auto" Height="Auto" x:Name="videoCapElement" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" StretchDirection="Both">
               
            </WPFMediaKit:VideoCaptureElement>
            <Button Canvas.Left="438" Canvas.Top="6" Content="Inverser" Height="23" Name="btRevertCam" Width="75" Click="btRevertCam_Click" HorizontalAlignment="Right" VerticalAlignment="Top" />

        </Canvas>

        <Image Height="142" Name="imgPreview" Stretch="Uniform" Canvas.Left="522" Canvas.Top="96" Margin="560,148,12,136" />
        <ComboBox Visibility="Collapsed" x:Name="videoCapDevices" ItemsSource="{Binding Source={x:Static WPFMediaKit:MultimediaUtil.VideoInputDevices}}" DisplayMemberPath="Name" Foreground="Black" Margin="171,0,183,289" />
        <Button Content="Photo" Height="23" HorizontalAlignment="Left" Margin="12,9,0,0" Name="btCapture" VerticalAlignment="Top" Width="75" Click="btCapture_Click" />
        <TextBox Height="23" Margin="93,9,45,0" Name="txtPhotoPath" VerticalAlignment="Top" />
        <Button Content="..." Height="23" Margin="0,8,12,0" Name="btFolderDialog" VerticalAlignment="Top" Click="btFolderDialog_Click" HorizontalAlignment="Right" Width="27" />
    </Grid>
</Window>

and codebehind :
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 System.IO;
using WPFMediaKit.DirectShow.Controls;
using System.Windows.Forms;
using WPFMediaKit.DirectShow.Interop;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        DsDevice dev1 = MultimediaUtil.VideoInputDevices.FirstOrDefault();
        DsDevice dev2 = MultimediaUtil.VideoInputDevices.Skip(1).FirstOrDefault();

        private void TakePic(VideoCaptureElement vce)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(txtPhotoPath.Text) && Directory.Exists(txtPhotoPath.Text))
                {
                    RenderTargetBitmap bmp = new RenderTargetBitmap(1280, 1024, 96, 96, PixelFormats.Default);
                    bmp.Render(vce);
                    imgPreview.Source = bmp;
                    BitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bmp));
                    string now = DateTime.Now.Year + "" + DateTime.Now.Month + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second;
                    string filename = System.IO.Path.Combine(txtPhotoPath.Text, now + "_pic.jpg");
                    FileStream fstream = new FileStream(filename, FileMode.Create);
                    encoder.Save(fstream);
                    fstream.Close();
                }
                else
                {
                    System.Windows.MessageBox.Show("veuillez choisir un chemin valide pour l'enregistrement des photos");
                }
            }
            catch (Exception ex)
            {

                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

                if (dev1 != null)
                {
                    videoCapElement.VideoCaptureDevice = dev1;
                    videoCapElement.VideoCaptureSource = dev1.Name;

                }
                if (fbd.SelectedPath == "") fbd.SelectedPath = Environment.CurrentDirectory;
                txtPhotoPath.Text = Environment.CurrentDirectory;
            }
            catch (Exception ex)
            {

                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void btCapture_Click(object sender, RoutedEventArgs e)
        {
            TakePic(videoCapElement);
        }


        private void btFolderDialog_Click(object sender, RoutedEventArgs e)
        {

            var dr = fbd.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                txtPhotoPath.Text = fbd.SelectedPath;
            }
        }

        private void btRevertCam_Click(object sender, RoutedEventArgs e)
        {
            if (dev1 != null && dev2 != null)
            {
                if (videoCapElement.VideoCaptureDevice == dev1)
                {
                    videoCapElement.VideoCaptureDevice = dev2;
                    videoCapElement.VideoCaptureSource = dev2.Name;
                }
                else
                {
                    videoCapElement.VideoCaptureDevice = dev1;
                    videoCapElement.VideoCaptureSource = dev1.Name;
                }
            }
        }
    }
}

Regards,