Advertisement
Blizzardo1

Open Source C# Camera App

Oct 1st, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.18 KB | None | 0 0
  1. /*If you see this posted as a guest, My apologies, Chrome didn't tell me that pastebin expired my session...*/
  2. /**
  3.     Created by: Adonis S. Deliannis
  4.     In support of ( ElineTeck.com )
  5.     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
  6.     Licensed under GPLv2
  7. **/
  8.  
  9. Window.xaml:
  10. <Window x:Class="Camera.MainWindow"
  11.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  12.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  13.         Title="BlizzetaDev CameraApp" Height="408" Width="502" Loaded="Window_Loaded" Background="#80000000" ResizeMode="NoResize">
  14.     <Grid>
  15.         <Menu Height="23" VerticalAlignment="Top">
  16.             <MenuItem Name="File" Header="_File">
  17.                 <MenuItem Name="Quit" Header="E_xit" Click="Quit_Click"/>
  18.             </MenuItem>
  19.             <MenuItem Name="Help" Header="_Help">
  20.                 <MenuItem Name="About" Header="_About" Click="About_Click"/>
  21.             </MenuItem>
  22.         </Menu>
  23.         <ListBox HorizontalAlignment="Left" Name="VideoDevices" Width="299" Margin="12,36,0,188" Height="129" VerticalAlignment="Center" />
  24.         <ListBox HorizontalAlignment="Left" Margin="12,196,0,28" Name="AudioDevices" Width="299" Height="129" VerticalAlignment="Center" />
  25.         <Label Content="Video Devices:" Height="28" HorizontalAlignment="Left" Margin="13,12,0,312" Name="label1" VerticalAlignment="Center">
  26.             <Label.Effect>
  27.                 <DropShadowEffect Color="White" ShadowDepth="0" Direction="0" BlurRadius="8" />
  28.             </Label.Effect>
  29.         </Label>
  30.         <Label Content="Audio Devices:" HorizontalAlignment="Left" Margin="12,171,0,154" Name="label2" Height="28" VerticalAlignment="Center" >
  31.             <Label.Effect>
  32.                 <DropShadowEffect Color="White" ShadowDepth="0" Direction="0" BlurRadius="8" />
  33.             </Label.Effect>
  34.         </Label>
  35.         <Label Content="Selected Video Device: null" Height="28" HorizontalAlignment="Left" Margin="317,36,0,0" Name="SVD" VerticalAlignment="Top" >
  36.             <Label.Effect>
  37.                 <DropShadowEffect Color="White" ShadowDepth="0" Direction="0" BlurRadius="8" />
  38.             </Label.Effect>
  39.         </Label>
  40.         <Label Content="Selected Audio Device: null" Height="28" HorizontalAlignment="Left" Margin="317,70,0,0" Name="SAD" VerticalAlignment="Top" >
  41.             <Label.Effect>
  42.                 <DropShadowEffect Color="White" ShadowDepth="0" Direction="0" BlurRadius="8" />
  43.             </Label.Effect>
  44.         </Label>
  45.         <Button Content="Preview" Height="23" HorizontalAlignment="Left" Margin="317,104,0,0" Name="button1" VerticalAlignment="Top" Width="153" Click="button1_Click">
  46.             <Button.Background>
  47.                 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
  48.                     <GradientStop Color="White" Offset="0.302"/>
  49.                     <GradientStop Color="Black" Offset="1" />
  50.                     <GradientStop Color="#FF313131" Offset="0.855" />
  51.                     <GradientStop Color="#FF373737" Offset="1" />
  52.                     <GradientStop Color="#FF3E3E3E" Offset="0.512" />
  53.                     <GradientStop Color="#FF212121" Offset="0.64" />
  54.                     <GradientStop Color="#FFA3A3A3" Offset="0" />
  55.                 </LinearGradientBrush>
  56.             </Button.Background>
  57.         </Button>
  58.         <Button Content="End Preview" Height="23" HorizontalAlignment="Left" Margin="317,133,0,0" Name="button2" VerticalAlignment="Top" Width="153" Click="button2_Click">
  59.             <Button.Background>
  60.                 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
  61.                     <GradientStop Color="White" Offset="0.302" />
  62.                     <GradientStop Color="Black" Offset="1" />
  63.                     <GradientStop Color="#FF313131" Offset="0.855" />
  64.                     <GradientStop Color="#FF373737" Offset="1" />
  65.                     <GradientStop Color="#FF3E3E3E" Offset="0.512" />
  66.                     <GradientStop Color="#FF212121" Offset="0.64" />
  67.                     <GradientStop Color="#FFA3A3A3" Offset="0" />
  68.                 </LinearGradientBrush>
  69.             </Button.Background>
  70.         </Button>
  71.        
  72.     </Grid>
  73. </Window>
  74.  
  75. Window.xaml.cs:
  76. using System;
  77. using System.Collections.Generic;
  78. using System.Linq;
  79. using System.Text;
  80. using System.Windows;
  81. using System.Windows.Controls;
  82. using System.Windows.Data;
  83. using System.Windows.Documents;
  84. using System.Windows.Input;
  85. using System.Windows.Interop;
  86. using System.Windows.Media;
  87. using System.Windows.Media.Imaging;
  88. using System.Windows.Navigation;
  89. using System.Windows.Shapes;
  90.  
  91. using Microsoft.Expression.Encoder;
  92. using Microsoft.Expression.Encoder.Devices;
  93. using Microsoft.Expression.Encoder.Live;
  94.  
  95. using BreakerDev.DwmApi;
  96. using BreakerDev.Imports32;
  97. using BreakerDev.Win32ErrorCodes;
  98. using BreakerDev.WPF_Interop;
  99.  
  100. namespace Camera
  101. {
  102.     /// <summary>
  103.     /// Interaction logic for MainWindow.xaml
  104.     /// </summary>
  105.     public partial class MainWindow : Window
  106.     {
  107.         LiveJob _job = null;
  108.         LiveDeviceSource lDevSrc = null;
  109.  
  110.         EncoderDevice video = null;
  111.         EncoderDevice audio = null;
  112.         System.Windows.Forms.Form CameraFrame = null;
  113.  
  114.  
  115.         public MainWindow()
  116.         {
  117.             InitializeComponent();
  118.         }
  119.  
  120.         class aBox : System.Windows.Forms.Form
  121.         {
  122.             public aBox()
  123.             {
  124.                 InitializeComponent();
  125.             }
  126.  
  127.             private void InitializeComponent()
  128.             {
  129.                 int w = 500, h = 260;
  130.                 richBox = new System.Windows.Forms.RichTextBox();
  131.  
  132.                 this.SuspendLayout();
  133.                 //
  134.                 // this Form
  135.                 //
  136.                
  137.                 this.Size = new System.Drawing.Size(w, h);
  138.                 this.Location = GetCentre();
  139.                 this.Text = "About the Camera App";
  140.                 //
  141.                 // richBox
  142.                 //
  143.                 this.richBox.Size = new System.Drawing.Size(w - 20 , h - 20);
  144.                 this.richBox.Location = GetWindowCentre(this.richBox);
  145.                 this.richBox.BackColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.ActiveCaption);
  146.                 this.richBox.Text = @"Thank you for trying this program!
  147. 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.
  148. Trip out by selecting your Screen at full resolution.
  149. Created By: Adonis Deliannis";
  150.                 this.richBox.TabIndex = 0;
  151.  
  152.                 this.ResumeLayout();
  153.  
  154.                 this.Controls.Add(this.richBox);
  155.             }
  156.  
  157.             private System.Drawing.Point GetCentre()
  158.             {
  159.                 int x = ((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2) - (this.Width / 2));
  160.                 int y = ((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2) - (this.Height / 2));
  161.                 return new System.Drawing.Point(x, y);
  162.             }
  163.  
  164.  
  165.             private System.Drawing.Point GetWindowCentre(System.Windows.Forms.Control c)
  166.             {
  167.                 int x = ((this.Width / 2) - (c.Width / 2));
  168.                 int y = ((this.Height / 2) - (c.Height / 2));
  169.                 return new System.Drawing.Point(x, y);
  170.             }
  171.  
  172.  
  173.             private System.Windows.Forms.RichTextBox richBox;
  174.         }
  175.  
  176.         private void Window_Loaded(object sender, RoutedEventArgs e)
  177.         {
  178.             IntPtr hWnd = GetHandle();
  179.             HwndSource src = HwndSource.FromHwnd(hWnd);
  180.             src.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
  181.  
  182.             Dwm.DWM_BLURBEHIND BB = new Dwm.DWM_BLURBEHIND();
  183.             BB.dwFlags = Dwm.DWM_BLURBEHIND.DWM_BB_BLURREGION | Dwm.DWM_BLURBEHIND.DWM_BB_ENABLE;
  184.             BB.fEnable = true;
  185.  
  186.             Dwm.DwmExtendFrameIntoClientArea(hWnd, new Dwm.MARGINS(-1, -1, -1, -1));
  187.             EnumerateDevices();
  188.         }
  189.  
  190.         private IntPtr GetHandle()
  191.         {
  192.             return (new WindowInteropHelper(this)).Handle;
  193.         }
  194.  
  195.         private void EnumerateDevices()
  196.         {
  197.             VideoDevices.Items.Clear();
  198.             AudioDevices.Items.Clear();
  199.  
  200.             foreach (EncoderDevice vDev in EncoderDevices.FindDevices(EncoderDeviceType.Video))
  201.                 VideoDevices.Items.Add(vDev.Name);
  202.  
  203.             foreach (EncoderDevice aDev in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
  204.                AudioDevices.Items.Add(aDev.Name);
  205.         }
  206.  
  207.         private bool BeginJob()
  208.         {
  209.             _job = new LiveJob();
  210.             int checkPoint = 0;
  211.             CameraFrame = new System.Windows.Forms.Form();
  212.  
  213.             foreach(EncoderDevice vD in EncoderDevices.FindDevices(EncoderDeviceType.Video))
  214.                 if (string.Compare(vD.Name, VideoDevices.SelectedItem.ToString()) == 0)
  215.                 {
  216.                     video = vD;
  217.                     SVD.Content = "Selected Video Device: " + vD.Name;
  218.                     checkPoint++;
  219.                     break;
  220.                 }
  221.  
  222.             foreach (EncoderDevice aD in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
  223.                 if (string.Compare(aD.Name, AudioDevices.SelectedItem.ToString()) == 0)
  224.                 {
  225.                     audio = aD;
  226.                     SAD.Content = "Selected Audio Device: " + aD.Name;
  227.                     checkPoint++;
  228.                     break;
  229.                 }
  230.             if (video != null && audio != null)
  231.             {
  232.                 lDevSrc = _job.AddDeviceSource(video, audio);
  233.                 lDevSrc.PickBestVideoFormat(new System.Drawing.Size(1920,1080), 60);
  234.  
  235.                 SourceProperties sp = lDevSrc.SourcePropertiesSnapshot();
  236.                 _job.OutputFormat.VideoProfile.Size = sp.Size;
  237.  
  238.                 CameraFrame.Size = sp.Size;
  239.  
  240.                 lDevSrc.PreviewWindow = new PreviewWindow(new System.Runtime.InteropServices.HandleRef(CameraFrame, CameraFrame.Handle));
  241.                 CameraFrame.Show();
  242.                 _job.ActivateSource(lDevSrc);
  243.             }
  244.             if (checkPoint >= 2)
  245.                 return true;
  246.             else
  247.                 return false;
  248.         }
  249.  
  250.         private void StopJob()
  251.         {
  252.             if (_job != null)
  253.             {
  254.                 _job.StopEncoding();
  255.                 _job.RemoveDeviceSource(lDevSrc);
  256.                 lDevSrc.PreviewWindow = null;
  257.                 lDevSrc = null;
  258.             }
  259.         }
  260.  
  261.         private void button1_Click(object sender, RoutedEventArgs e)
  262.         {
  263.             BeginJob();
  264.             button1.IsEnabled = false;
  265.             button2.IsEnabled = true;
  266.         }
  267.  
  268.         private void button2_Click(object sender, RoutedEventArgs e)
  269.         {
  270.             StopJob();
  271.             button1.IsEnabled = true;
  272.             button2.IsEnabled = false;
  273.         }
  274.  
  275.         private void Quit_Click(object sender, RoutedEventArgs e)
  276.         {
  277.             Imports.Kernel32.Quit(0x00);
  278.         }
  279.  
  280.         private void About_Click(object sender, RoutedEventArgs e)
  281.         {
  282.             (new aBox()).ShowDialog();
  283.         }
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement