现在位置首页 / 项目实战 /正文

WPF控件--模拟surface pro 2自带的拍照效果的样式

作者: IT小兵 | 2014年7月29日| 热度:℃ | 评论: |参与:

今天分享一个wpf Camera照相的样式,模拟surface pro2 的系统自带的样式,有个差别就是按钮没有悬浮。

可能是我用的拍照插件AForge.net的问题。

效果图:

界面ui xmal代码:

<Window x:Class="UI.Controls.FaceCapture"
        xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:Splash;assembly=FingerPictureBox"
        Title="FaceCapture(WPF)" mc:Ignorable="d" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        Height="700" Width="1270" WindowStyle="None"
        WindowStartupLocation="CenterScreen"   AllowDrop="True" Closing="Window_Closing" ResizeMode="NoResize">
    <Grid AllowDrop="True" Background="#000000">
        
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="70"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" Grid.Row="0" AllowDrop="False" Visibility="Hidden" Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="350" />
                <ColumnDefinition Width="350" />
                <ColumnDefinition Width="350" />
				<ColumnDefinition Width="220" />
            </Grid.ColumnDefinitions>      
            <Button  Grid.Row="0"  Grid.Column="0"  Height="40" Width="180" >
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                    <Label Content="切换摄像头" VerticalContentAlignment="Center" FontSize="14" />
                </StackPanel>
            </Button>
            <Button  Grid.Row="0"  Grid.Column="1"    Height="40" Width="180" >
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                    <Label Content="拍摄图像" Name="lb_text" VerticalContentAlignment="Center" FontSize="14" />
                </StackPanel>
            </Button>
            <Button  Grid.Row="0"   Grid.Column="2"  Height="40" Width="180"   >
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                    <Label Content="保存图像" VerticalContentAlignment="Center" FontSize="14" />
                </StackPanel>
            </Button>
        </Grid>
        
        
        
        <wfi:WindowsFormsHost Name="cam_sumit" Grid.Row="0" Grid.Column="0" >
            <aforge:VideoSourcePlayer x:Name="sourcePlayer" Width="1200" Height="700">
            </aforge:VideoSourcePlayer>
        </wfi:WindowsFormsHost>
        <my:FingerPictureBox  x:Name="fingerPictureBox1"  Visibility="Hidden" Grid.Row="1" Height="700" Width="1200"  BorderThickness="1" BorderBrush="DarkGreen" Grid.ColumnSpan="2" Margin="0,0,0,-100"  />
		<Border Grid.Column="1" BorderThickness="1,0,0,0" BorderBrush="#333333">
		<StackPanel x:Name="photoAction"    Panel.ZIndex="1000000000" VerticalAlignment="Center" Width="70" HorizontalAlignment="Right"  Margin="0,75">
			<Button Width="60" Height="90"  Style="{DynamicResource BtnPhotoPhoto}" Name="button_Qh" Content="切换相机" Click="button_Qh_Click">
				<Button.Background>
					<ImageBrush ImageSource="../Themes/ZCThemes/skin/btn/photo_switch_on.png" Stretch="None"/>
				</Button.Background></Button>
			<Button Width="60" Height="90"  Margin="0,60,0,0" IsEnabled="False" Content="拍照" Style="{DynamicResource BtnPhotoPhoto}" Name="button_Capture" Click="button_Capture_Click">
				<Button.BorderBrush>
					<ImageBrush ImageSource="../Themes/ZCThemes/skin/btn/photo_photo_disable.png" Stretch="None"/>
				</Button.BorderBrush>
				<Button.Background>
					<ImageBrush ImageSource="../Themes/ZCThemes/skin/btn/photo_photo.png" Stretch="None"/>
				</Button.Background></Button>
			<Button Width="60" Height="90"  Margin="0,60,0,0" Content="保存照片" Style="{DynamicResource BtnPhotoPhoto}" Name="button_Close" Click="button_Close_Click">
				<Button.Background>
					<ImageBrush ImageSource="../Themes/ZCThemes/skin/btn/photo_save.png" Stretch="None"/>
				</Button.Background></Button>
            <Button Width="60" Height="90" Click="btnclose_Click" Content="退出"  x:Name="btnclose"  Margin="0,60,0,0" Style="{DynamicResource BtnPhotoPhoto}" >
			<Button.Background>
					<ImageBrush ImageSource="../Themes/ZCThemes/skin/btn/photo_close.png" Stretch="None"/>
				</Button.Background></Button>
		</StackPanel>
		</Border>
    </Grid>
</Window>

用到的图片:

百度网盘下载地址:链接: http://pan.baidu.com/s/1kTiK8Gf   密码: 6k0b 

后台代码,有需求的拿去,和上次分享的代码差不多[wpf教程-AForge.net调用摄像头/切换摄像头/保存图片及像素问题],多了一个循环切换摄像头的功能:

using System;
using System.Windows;
using System.Windows.Media.Imaging;
using AForge.Video.DirectShow;
using Splash;
using System.Windows.Controls;
using System.Windows.Media;
using System.IO;
using System.Collections.Generic;
using System.Windows.Documents;
namespace  UI.Controls
{
    public delegate void CameraDoHandler(string photopath);
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FaceCapture : Window
    {
        public event CameraDoHandler CameraDoEvent;
        public byte[] CaptureData { get; set; }
        FilterInfoCollection videoDevices = null;
        public int xcount = 0;
        public int j = 0;
        public FaceCapture()
        {
            InitializeComponent();
           // 设置窗体装载后事件处理器
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // 设定初始视频设备
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            xcount = videoDevices.Count;
            if (videoDevices.Count > 0)
            {   // 默认设备
                sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            }
            sourcePlayer.Start();
           // 设置图片框初始图像
            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                Properties.Resources.noimage.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            fingerPictureBox1.InitialImage = bs;          
            if (sourcePlayer.IsRunning)
            {
                button_Capture.IsEnabled = true;
            }
        }
        /// <summary>
        /// 切换摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Qh_Click(object sender, RoutedEventArgs e)
        {
            if (j < xcount-1)
            {
                j = j + 1;
            }
            else if (j == xcount-1)
            {
                j = 0;            
            }
            sourcePlayer.Stop();
            sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[j].MonikerString);
            sourcePlayer.Start();
        }
        private void btnclose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 拍摄图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Capture_Click(object sender, RoutedEventArgs e)
        {
            // 判断视频设备是否开启
            try
            {
                if (sourcePlayer.IsRunning)
                {   // 进行拍照
                    if (fingerPictureBox1.Visibility == Visibility.Hidden)
                    {
                        object box = this.FindName("fingerPictureBox1");
                        if (box is FingerPictureBox)
                        {
                            (box as FingerPictureBox).ActiveImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                sourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
                                IntPtr.Zero,
                                Int32Rect.Empty,
                                BitmapSizeOptions.FromEmptyOptions());
                        }
                        fingerPictureBox1.Visibility = Visibility.Visible;
                        cam_sumit.Visibility = Visibility.Hidden;
                        lb_text.Content = "启动摄像头";
                    }
                    else {
                        fingerPictureBox1.Visibility = Visibility.Hidden;
                        cam_sumit.Visibility = Visibility.Visible;
                        lb_text.Content = "拍摄图像";                       
                    }
                }
            }
            catch {
                MessageBox.Show("请等待摄像头准备就绪再拍照!");
            }
        }
        /// <summary>
        /// 保存返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Close_Click(object sender, RoutedEventArgs e)
        {
            var tempfilepath = "";
            RenderTargetBitmap bmp = new RenderTargetBitmap(
            (int)fingerPictureBox1.ActualWidth, (int)fingerPictureBox1.ActualHeight,
            96, 96, PixelFormats.Default);
            fingerPictureBox1.Measure(fingerPictureBox1.RenderSize);
            fingerPictureBox1.Arrange(new Rect(fingerPictureBox1.RenderSize));
            bmp.Render(fingerPictureBox1);
            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                CaptureData = ms.ToArray();
                var  temppath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyy-mm-dd-hh-mm-sss") + ".jpg";
                File.WriteAllBytes(temppath, CaptureData);
                tempfilepath = FileOperation.AddFile(Common.Contract.WebModel.AyncModuleEnum.系统拍照, temppath, true);
            }
            if (CameraDoEvent != null)
            {
                CameraDoEvent(tempfilepath);
            }
            this.Close();
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (sourcePlayer.IsRunning)
            {   // 停止视频
                sourcePlayer.SignalToStop();
                sourcePlayer.WaitForStop();
                sourcePlayer.Stop();
            }
        }
    }
}



点击阅读本文所属分类的更多文章: 项目实战 。和高手一起交流:346717337
友荐云推荐

未注明转发、原文均为本站原创。分享本文请注明 原文链接

给您更多信息和帮助

在这里您可以找到更多:

技术交流群:346717337 Jquery插件交流

投稿:suchso@vip.qq.com

承接:企业网站门户/微网站/微商城/CMS系统/微信公众号运营/业务咨询

echarts教程系列
本月最热文章

微信扫一扫,徜徉悠嘻网,您的休闲乐园

微信公众号:快乐每一天

随机文章
标签

技术交流群:346717337

投稿:suchso@vip.qq.com

专业专注:企业网站门户/微网站/微商城/CMS系统/微信公众号运营/付费问题咨询