Blizzardo1 icon

Open Source C# Camera App

Blizzardo1 | PRO | 10/01/12 06:02:19 AM UTC | 0 ⭐ | 725 👁️ | Never ⏰ | []
C# |

11.18 KB

|

None

|

0 👍

/

0 👎

/*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 x:Class="Camera.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="BlizzetaDev CameraApp" Height="408" Width="502" Loaded="Window_Loaded" Background="#80000000" ResizeMode="NoResize">
    <Grid>
        <Menu Height="23" VerticalAlignment="Top">
            <MenuItem Name="File" Header="_File">
                <MenuItem Name="Quit" Header="E_xit" Click="Quit_Click"/>
            </MenuItem>
            <MenuItem Name="Help" Header="_Help">
                <MenuItem Name="About" Header="_About" Click="About_Click"/>
            </MenuItem>
        </Menu>
        <ListBox HorizontalAlignment="Left" Name="VideoDevices" Width="299" Margin="12,36,0,188" Height="129" VerticalAlignment="Center" />
        <ListBox HorizontalAlignment="Left" Margin="12,196,0,28" Name="AudioDevices" Width="299" Height="129" VerticalAlignment="Center" />
        <Label Content="Video Devices:" Height="28" HorizontalAlignment="Left" Margin="13,12,0,312" Name="label1" VerticalAlignment="Center">
            <Label.Effect>
                <DropShadowEffect Color="White" ShadowDepth="0" Direction="0" BlurRadius="8" />
            </Label.Effect>
        </Label>
        <Label Content="Audio Devices:" HorizontalAlignment="Left" Margin="12,171,0,154" Name="label2" Height="28" VerticalAlignment="Center" >
            <Label.Effect>
                <DropShadowEffect Color="White" ShadowDepth="0" Direction="0" BlurRadius="8" />
            </Label.Effect>
        </Label>
        <Label Content="Selected Video Device: null" Height="28" HorizontalAlignment="Left" Margin="317,36,0,0" Name="SVD" VerticalAlignment="Top" >
            <Label.Effect>
                <DropShadowEffect Color="White" ShadowDepth="0" Direction="0" BlurRadius="8" />
            </Label.Effect>
        </Label>
        <Label Content="Selected Audio Device: null" Height="28" HorizontalAlignment="Left" Margin="317,70,0,0" Name="SAD" VerticalAlignment="Top" >
            <Label.Effect>
                <DropShadowEffect Color="White" ShadowDepth="0" Direction="0" BlurRadius="8" />
            </Label.Effect>
        </Label>
        <Button Content="Preview" Height="23" HorizontalAlignment="Left" Margin="317,104,0,0" Name="button1" VerticalAlignment="Top" Width="153" Click="button1_Click">
            <Button.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="White" Offset="0.302"/>
                    <GradientStop Color="Black" Offset="1" />
                    <GradientStop Color="#FF313131" Offset="0.855" />
                    <GradientStop Color="#FF373737" Offset="1" />
                    <GradientStop Color="#FF3E3E3E" Offset="0.512" />
                    <GradientStop Color="#FF212121" Offset="0.64" />
                    <GradientStop Color="#FFA3A3A3" Offset="0" />
                </LinearGradientBrush>
            </Button.Background>
        </Button>
        <Button Content="End Preview" Height="23" HorizontalAlignment="Left" Margin="317,133,0,0" Name="button2" VerticalAlignment="Top" Width="153" Click="button2_Click">
            <Button.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="White" Offset="0.302" />
                    <GradientStop Color="Black" Offset="1" />
                    <GradientStop Color="#FF313131" Offset="0.855" />
                    <GradientStop Color="#FF373737" Offset="1" />
                    <GradientStop Color="#FF3E3E3E" Offset="0.512" />
                    <GradientStop Color="#FF212121" Offset="0.64" />
                    <GradientStop Color="#FFA3A3A3" Offset="0" />
                </LinearGradientBrush>
            </Button.Background>
        </Button>
        
    </Grid>
</Window>
 
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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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();
        }
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎