AForge.NET 实现简单的拍照

<metro:Window x:Class="FlashGame.ProtraitWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:metro="http://schemas.codeplex.com/elysium"
        xmlns:params="http://schemas.codeplex.com/elysium/params"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:cc="clr-namespace:CustomerControls;assembly=CustomerControls"
        xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
        params:Manager.Theme="Dark"
        params:Manager.AccentBrush="{x:Static metro:AccentBrushes.Blue}"
        params:Manager.ContrastBrush="White"
        Background="{DynamicResource BackgroundBrush}"
        xmlns:local="clr-namespace:FlashGame"
        Title="个人头像" Height="500"  Width="824" WindowStartupLocation="CenterOwner" Closing="Window_Closing">
                    
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <wfi:WindowsFormsHost Margin="5">
                <aforge:VideoSourcePlayer x:Name="sourcePlayer" Width="300" Height="360" />
            </wfi:WindowsFormsHost>
            <StackPanel Margin="5" Grid.Row="1" Orientation="Horizontal" Height="60" HorizontalAlignment="Stretch" >
                <metro:CommandButton x:Name="btnPlay" Click="btnPlay_Click" Tag="0" Header="启动摄像头" metro:ApplicationBar.Dock="Right" Margin="5">
                    <Image Height="32" Width="32" Source="pack://application:,,,/Icons;Component/wp/dark/appbar.control.play.png" />
                </metro:CommandButton>
                <metro:CommandButton x:Name="btnCapture" Click="btnCapture_Click" Header="赞助" metro:ApplicationBar.Dock="Right" Margin="5">
                    <Image Height="32" Width="32" Source="pack://application:,,,/Icons;Component/wp/dark/appbar.camera.png" />
                </metro:CommandButton>
            </StackPanel>
        </Grid>
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Orientation="Horizontal">
                <!--<WindowsFormsHost  >
                    <aforge:PictureBox x:Name="pb1"/>
                </WindowsFormsHost>
                <WindowsFormsHost >
                    <aforge:PictureBox x:Name="pb2"/>
                </WindowsFormsHost>-->
                <Image x:Name="pb1" Width="200" Height="200" Stretch="Fill"/>
                <Image x:Name="pb2" Width="200" Height="200" Stretch="Fill"/>
            </StackPanel>
            <StackPanel Grid.Row="1" Orientation="Horizontal">
                <!--<WindowsFormsHost  >
                    <aforge:PictureBox x:Name="pb3"/>
                </WindowsFormsHost>
                <WindowsFormsHost >
                    <aforge:PictureBox x:Name="pb4"/>
                </WindowsFormsHost>-->
                <Image x:Name="pb3" Width="200" Height="200" Stretch="Fill"/>
                <Image x:Name="pb4" Width="200" Height="200" Stretch="Fill"/>
            </StackPanel>
        </Grid>
    </Grid>
                    
                    
    <metro:Window.TitleBar>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="1"
             Content="个人头像" x:Name="AboutButton"
             Style="{DynamicResource {x:Static metro:Window.DefaultCaptionButtonStyleKey}}"/>
        </Grid>
    </metro:Window.TitleBar>
    <metro:Window.ApplicationBar>
        <metro:ApplicationBar Padding="5">
            <metro:CommandButton IsEnabled="False" Header="赞助" metro:ApplicationBar.Dock="Right" Margin="5">
                <Image Height="32" Width="32" Source="pack://application:,,,/Icons;Component/wp/dark/appbar.camera.flash.auto.png" />
            </metro:CommandButton>
            <metro:CommandButton IsEnabled="False" Header="赞助" metro:ApplicationBar.Dock="Right" Margin="5">
                <Image Height="32" Width="32" Source="pack://application:,,,/Icons;Component/wp/dark/appbar.camera.flash.png" />
            </metro:CommandButton>
            <metro:CommandButton IsEnabled="False" Header="赞助" metro:ApplicationBar.Dock="Right" Margin="5">
                <Image Height="32" Width="32" Source="pack://application:,,,/Icons;Component/wp/dark/appbar.camera.flash.off.png" />
            </metro:CommandButton>
        </metro:ApplicationBar>
    </metro:Window.ApplicationBar>
</metro:Window>

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.Shapes;
using AForge.Video.DirectShow;
using Elysium.Controls;
using AForge.Controls;
namespace FlashGame
{
    /// <summary>
    /// ProtraitWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ProtraitWindow : Elysium.Controls.Window
    {
        BitmapSource ImagePlay;
        BitmapSource ImageStop;
        bool IsPlaying = false;
        int CurrentIndex = 0;
        public ProtraitWindow()
        {
            InitializeComponent();
            // 设置窗体装载后事件处理器
            this.Loaded += new RoutedEventHandler(ProtraitWindow_Loaded);
        }
        void ProtraitWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // 设定初始视频设备
            FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count > 0)
            {   // 默认设备
                sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            }
            else
            {
                //button_Play.IsEnabled = false;
                //button_Capture.IsEnabled = false;
            }
        }
        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            if (IsPlaying)
            {
                if (sourcePlayer.IsRunning)
                {   // 停止视频
                    sourcePlayer.SignalToStop();
                    sourcePlayer.WaitForStop();
                }
                IsPlaying = false;
                ((sender as CommandButton).Content as Image).Source = new BitmapImage(new Uri("pack://application:,,,/Icons;Component/wp/dark/appbar.control.play.png"));
            }
            else
            {
                sourcePlayer.Start();
                ((sender as CommandButton).Content as Image).Source = new BitmapImage(new Uri("pack://application:,,,/Icons;Component/wp/dark/appbar.control.stop.png"));
                IsPlaying = true;
            }
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (sourcePlayer.IsRunning)
            {   // 停止视频
                sourcePlayer.SignalToStop();
                sourcePlayer.WaitForStop();
            }
        }
        private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            // 判断视频设备是否开启
            if (sourcePlayer.IsRunning)
            {   // 进行拍照
                for (Int32 i = 1; i <= 4; i++)
                {
                    object box = this.FindName("pb" + i);
                    if (box is Image )
                    {
                        //为空
                        if ((box as Image).Source == null)
                        {
                            // 更新图像
                            (box as Image).Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                sourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
                                IntPtr.Zero,
                                Int32Rect.Empty,
                                BitmapSizeOptions.FromEmptyOptions());
                            break;
                        }
                               
                    }
                }
            }
        }
    }
}

点击查看原图

知识共享许可协议
《AForge.NET 实现简单的拍照》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     168.76ms