/*If you see this posted as a guest, My apologies, Chrome didn't tell me that pastebin expired my session...*/ /** Created by: Adonis S. Deliannis In support of ( ElineTeck.com ) Example written by: Adonis S. Deliannis, Regards to http://www.codeproject.com/Articles/202464/How-to-use-a-WebCam-in-C-with-the-NET-Framework-4 Licensed under GPLv2 **/ Window.xaml: Window.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.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Microsoft.Expression.Encoder; using Microsoft.Expression.Encoder.Devices; using Microsoft.Expression.Encoder.Live; using BreakerDev.DwmApi; using BreakerDev.Imports32; using BreakerDev.Win32ErrorCodes; using BreakerDev.WPF_Interop; namespace Camera { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { LiveJob _job = null; LiveDeviceSource lDevSrc = null; EncoderDevice video = null; EncoderDevice audio = null; System.Windows.Forms.Form CameraFrame = null; public MainWindow() { InitializeComponent(); } class aBox : System.Windows.Forms.Form { public aBox() { InitializeComponent(); } private void InitializeComponent() { int w = 500, h = 260; richBox = new System.Windows.Forms.RichTextBox(); this.SuspendLayout(); // // this Form // this.Size = new System.Drawing.Size(w, h); this.Location = GetCentre(); this.Text = "About the Camera App"; // // richBox // this.richBox.Size = new System.Drawing.Size(w - 20 , h - 20); this.richBox.Location = GetWindowCentre(this.richBox); this.richBox.BackColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.ActiveCaption); this.richBox.Text = @"Thank you for trying this program! Thanks to http://www.codeproject.com/Articles/202464/How-to-use-a-WebCam-in-C-with-the-NET-Framework-4 for helping me write this. Trip out by selecting your Screen at full resolution. Created By: Adonis Deliannis"; this.richBox.TabIndex = 0; this.ResumeLayout(); this.Controls.Add(this.richBox); } private System.Drawing.Point GetCentre() { int x = ((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2) - (this.Width / 2)); int y = ((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2) - (this.Height / 2)); return new System.Drawing.Point(x, y); } private System.Drawing.Point GetWindowCentre(System.Windows.Forms.Control c) { int x = ((this.Width / 2) - (c.Width / 2)); int y = ((this.Height / 2) - (c.Height / 2)); return new System.Drawing.Point(x, y); } private System.Windows.Forms.RichTextBox richBox; } private void Window_Loaded(object sender, RoutedEventArgs e) { IntPtr hWnd = GetHandle(); HwndSource src = HwndSource.FromHwnd(hWnd); src.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0); Dwm.DWM_BLURBEHIND BB = new Dwm.DWM_BLURBEHIND(); BB.dwFlags = Dwm.DWM_BLURBEHIND.DWM_BB_BLURREGION | Dwm.DWM_BLURBEHIND.DWM_BB_ENABLE; BB.fEnable = true; Dwm.DwmExtendFrameIntoClientArea(hWnd, new Dwm.MARGINS(-1, -1, -1, -1)); EnumerateDevices(); } private IntPtr GetHandle() { return (new WindowInteropHelper(this)).Handle; } private void EnumerateDevices() { VideoDevices.Items.Clear(); AudioDevices.Items.Clear(); foreach (EncoderDevice vDev in EncoderDevices.FindDevices(EncoderDeviceType.Video)) VideoDevices.Items.Add(vDev.Name); foreach (EncoderDevice aDev in EncoderDevices.FindDevices(EncoderDeviceType.Audio)) AudioDevices.Items.Add(aDev.Name); } private bool BeginJob() { _job = new LiveJob(); int checkPoint = 0; CameraFrame = new System.Windows.Forms.Form(); foreach(EncoderDevice vD in EncoderDevices.FindDevices(EncoderDeviceType.Video)) if (string.Compare(vD.Name, VideoDevices.SelectedItem.ToString()) == 0) { video = vD; SVD.Content = "Selected Video Device: " + vD.Name; checkPoint++; break; } foreach (EncoderDevice aD in EncoderDevices.FindDevices(EncoderDeviceType.Audio)) if (string.Compare(aD.Name, AudioDevices.SelectedItem.ToString()) == 0) { audio = aD; SAD.Content = "Selected Audio Device: " + aD.Name; checkPoint++; break; } if (video != null && audio != null) { lDevSrc = _job.AddDeviceSource(video, audio); lDevSrc.PickBestVideoFormat(new System.Drawing.Size(1920,1080), 60); SourceProperties sp = lDevSrc.SourcePropertiesSnapshot(); _job.OutputFormat.VideoProfile.Size = sp.Size; CameraFrame.Size = sp.Size; lDevSrc.PreviewWindow = new PreviewWindow(new System.Runtime.InteropServices.HandleRef(CameraFrame, CameraFrame.Handle)); CameraFrame.Show(); _job.ActivateSource(lDevSrc); } if (checkPoint >= 2) return true; else return false; } private void StopJob() { if (_job != null) { _job.StopEncoding(); _job.RemoveDeviceSource(lDevSrc); lDevSrc.PreviewWindow = null; lDevSrc = null; } } private void button1_Click(object sender, RoutedEventArgs e) { BeginJob(); button1.IsEnabled = false; button2.IsEnabled = true; } private void button2_Click(object sender, RoutedEventArgs e) { StopJob(); button1.IsEnabled = true; button2.IsEnabled = false; } private void Quit_Click(object sender, RoutedEventArgs e) { Imports.Kernel32.Quit(0x00); } private void About_Click(object sender, RoutedEventArgs e) { (new aBox()).ShowDialog(); } } }