<Window x:Name="mainWindow" x:Class="WPFCSharpWebCam.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF C# WebCam" Height="400" Width="600" Loaded="mainWindow_Loaded">
<!-- Design by Pongsakorn Poosankam -->
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFD7F4E8" Offset="0.916"/>
<GradientStop Color="#FF6CB595" Offset="0.145"/>
</LinearGradientBrush>
</Window.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.11*"/>
<RowDefinition Height="0.442*"/>
<RowDefinition Height="0.067*"/>
<RowDefinition Height="0.088*"/>
<RowDefinition Height="0.293*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.068*"/>
<ColumnDefinition Width="0.274*"/>
<ColumnDefinition Width="0.069*"/>
<ColumnDefinition Width="0.274*"/>
<ColumnDefinition Width="0.315*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="1" Grid.Row="1" BorderThickness="3" CornerRadius="3">
<Border.BorderBrush>
<RadialGradientBrush>
<GradientStop Color="Black" Offset="0.047"/>
<GradientStop Color="#FF00907A" Offset="1"/>
</RadialGradientBrush>
</Border.BorderBrush>
<Image x:Name="imgVideo" Stretch="Fill" />
</Border>
<Border Grid.Column="3" Grid.Row="1" BorderThickness="3" CornerRadius="3">
<Border.BorderBrush>
<RadialGradientBrush>
<GradientStop Color="Black" Offset="0.047"/>
<GradientStop Color="#FF00907A" Offset="1"/>
</RadialGradientBrush>
</Border.BorderBrush>
<Image x:Name="imgCapture" Stretch="Fill"/>
</Border>
<StackPanel Grid.Column="3" Grid.Row="3" Orientation="Horizontal">
<Button x:Name="bntCapture" Content="Capture Image" Click="bntCapture_Click" />
<Button x:Name="bntSaveImage" Content="Save Image" Margin="8,0,0,0" Click="bntSaveImage_Click" />
</StackPanel>
<StackPanel Grid.Column="4" Grid.Row="1" VerticalAlignment="Center">
<Button x:Name="bntResolution" Content="Video Format" Width="120" Click="bntResolution_Click" />
<Button x:Name="bntSetting" Content="Video Source" Width="120" Margin="0,10,0,0" Click="bntSetting_Click" />
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="bntStart" Content="Start" Click="bntStart_Click" />
<Button x:Name="bntStop" Content="Stop" Margin="10,0,0,0" Click="bntStop_Click" />
<Button x:Name="bntContinue" Content="Continue" Margin="10,0,0,0" Click="bntContinue_Click" />
</StackPanel>
</Grid>
</Window>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Be hide the Code Windows1.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;
namespace WPFCSharpWebCam
{
/// <summary>
/// Design by Pongsakorn Poosankam
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
WebCam webcam;
private void mainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// TODO: Add event handler implementation here.
webcam = new WebCam();
webcam.InitializeWebCam(ref imgVideo);
}
private void bntStart_Click(object sender, RoutedEventArgs e)
{
webcam.Start();
}
private void bntStop_Click(object sender, RoutedEventArgs e)
{
webcam.Stop();
}
private void bntContinue_Click(object sender, RoutedEventArgs e)
{
webcam.Continue();
}
private void bntCapture_Click(object sender, RoutedEventArgs e)
{
imgCapture.Source = imgVideo.Source;
}
private void bntSaveImage_Click(object sender, RoutedEventArgs e)
{
Helper.SaveImageCapture((BitmapSource)imgCapture.Source);
}
private void bntResolution_Click(object sender, RoutedEventArgs e)
{
webcam.ResolutionSetting();
}
private void bntSetting_Click(object sender, RoutedEventArgs e)
{
webcam.AdvanceSetting();
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Class for Control Web cam WebCam.cs
This class is main webcam controller for real time capturing. Developer can change frame rate at here.
using System;
using System.IO;
using System.Linq;
using System.Text;
using WebCam_Capture;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
namespace WPFCSharpWebCam
{
//Design by Pongsakorn Poosankam
class WebCam
{
private WebCamCapture webcam;
private System.Windows.Controls.Image _FrameImage;
private int FrameNumber = 30;
public void InitializeWebCam(ref System.Windows.Controls.Image FingerImage)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = FingerImage;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Source = Helper.LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);
}
public void Start()
{
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.Start(0);
}
public void Stop()
{
webcam.Stop();
}
public void Continue()
{
// change the capture time frame
webcam.TimeToCapture_milliseconds = FrameNumber;
// resume the video capture from the stop
webcam.Start(this.webcam.FrameNumber);
}
public void ResolutionSetting()
{
webcam.Config();
}
public void AdvanceSetting()
{
webcam.Config2();
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Helper class Helper.cs
This class use for convert Bitmap Image from webcam to BitmapSource Object.
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
namespace WPFCSharpWebCam
{
//Design by Pongsakorn Poosankam
class Helper
{
//Block Memory Leak
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr handle);
public static BitmapSource bs;
public static IntPtr ip;
public static BitmapSource LoadBitmap(System.Drawing.Bitmap source)
{
ip = source.GetHbitmap();
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ip);
return bs;
}
public static void SaveImageCapture(BitmapSource bitmap)
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.QualityLevel = 100;
// Configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Image"; // Default file name
dlg.DefaultExt = ".Jpg"; // Default file extension
dlg.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
FileStream fstream = new FileStream(filename, FileMode.Create);
encoder.Save(fstream);
fstream.Close();
}
}
}
}
If you found any problem please report to me by comment at below (login with gmail account before comment).
Thank you for try.