■ 웹 카메라를 사용하는 방법을 보여준다.
※ 웹 카메라 캡처 부분을 캡처할 수 없었다.
[TestProject 프로젝트]
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:library="clr-namespace:TestLibrary;assembly=TestLibrary" Width="800" Height="600" MinWidth="400" MinHeight="300" Title="웹 카메라 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Border Grid.Row="0" BorderThickness="1" BorderBrush="Black"> <library:WebCameraControl x:Name="webCameraControl" /> </Border> <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <ComboBox x:Name="videoCaptureDeviceComboBox" Margin="0 5 0 5" Width="200" Height="30" VerticalContentAlignment="Center" DisplayMemberPath="Name" /> <Button x:Name="startButton" Margin="20 5 0 5" Width="100" Height="30" IsEnabled="True" Content="시작하기" /> <Button x:Name="stopButton" Margin="10 5 0 5" Width="100" Height="30" Content="중단하기" IsEnabled="{Binding Path=IsCapturing, ElementName=webCameraControl}" /> <Button x:Name="imageButton" Margin="10 5 0 5" Width="100" Height="30" Content="이미지" IsEnabled="{Binding Path=IsCapturing, ElementName=webCameraControl}" /> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
using Microsoft.Win32; using System.Windows; using System.Windows.Controls; using TestLibrary; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.videoCaptureDeviceComboBox.ItemsSource = this.webCameraControl.GetVideoCaptureDeviceList(); if(this.videoCaptureDeviceComboBox.Items.Count > 0) { this.videoCaptureDeviceComboBox.SelectedItem = this.videoCaptureDeviceComboBox.Items[0]; } this.videoCaptureDeviceComboBox.SelectionChanged += videoCaptureDeviceComboBox_SelectionChanged; this.startButton.Click += startButton_Click; this.stopButton.Click += stopButton_Click; this.imageButton.Click += imageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 비디오 캡처 장치 콤보 박스 선택 변경시 처리하기 - videoCaptureDeviceComboBox_SelectionChanged(sender, e) /// <summary> /// 비디오 캡처 장치 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void videoCaptureDeviceComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { this.startButton.IsEnabled = e.AddedItems.Count > 0; } #endregion #region 시작하기 버튼 클릭시 처리하기 - startButton_Click(sender, e) /// <summary> /// 시작하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void startButton_Click(object sender, RoutedEventArgs e) { VideoCaptureDevice videoCaptureDevice = (VideoCaptureDevice)this.videoCaptureDeviceComboBox.SelectedItem; this.webCameraControl.StartCapture(videoCaptureDevice); this.startButton.IsEnabled = false; } #endregion #region 중단하기 버튼 클릭시 처리하기 - startButton_Click(sender, e) /// <summary> /// 중단하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stopButton_Click(object sender, RoutedEventArgs e) { this.webCameraControl.StopCapture(); this.startButton.IsEnabled = true; } #endregion #region 이미지 버튼 클릭시 처리하기 - startButton_Click(sender, e) /// <summary> /// 이미지 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void imageButton_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = "비트맵 이미지|*.bmp" }; if(saveFileDialog.ShowDialog() == true) { this.webCameraControl.GetCurrentImage().Save(saveFileDialog.FileName); } } #endregion } } |