vlc 視頻局部放大【WPF版】

        最近作了一個項目,其中功能有二維、三維地圖展現,視頻、音頻播放,視頻解碼同步音頻等,項目過程當中也遇到一些技術難題,但最終仍是解決,我我的感受最應該記錄下來的就是視頻局放大的功能。先上兩張效果圖:express

        根據項目需求,須要對視頻進行局部放大,便於對細節地方進行查看,視頻播放控件採用的是xZune.Vlc.Wpf - .Net 4.5VlcMediaPlayer網上有開源的代碼,由於須要對視頻的音頻輸出端口進行設置,而不是經過默認的揚聲器輸出,設置音頻設置端口爲:app

        this.videoPlayer.Initialize(libVlcPath, options);       ide

        libVlcPath爲libvlc庫文件夾路徑;options爲參數,包括音頻輸出設備(如:"-I", "dummy", "--avi-index=1", "--ignore-config", "--no-video-title", "--aout=directsound")

        視頻局部放大主要參考網上有位朋友發的wpf圖片局部放大的原理來實現,主要實現步驟以下:佈局

        1、佈局this

 <!--右側大圖-->
        <Canvas x:Name="BigBox" Width="643" Height="643" Canvas.Left="360" Canvas.Top="20" Visibility="Visible">
            <!--右側原圖片 注意尺寸-->
            <Image x:Name="bigImg" Width="2592" Height="1922" Canvas.Left="0" Canvas.Top="-780" />
            <Canvas.Clip>
                <RectangleGeometry Rect="0,0,643,643" />
            </Canvas.Clip>
        </Canvas>


        <!--左側小圖-->
        <Canvas x:Name="SmallBox" HorizontalAlignment="Left" VerticalAlignment="Top" MouseMove="MoveRect_MouseMove" Width="121" Height="90" Canvas.Left="20" Canvas.Top="20">
            <Canvas.Background>
                <ImageBrush Stretch="UniformToFill" />
            </Canvas.Background>
            <!--半透明矩形框-->
            <Rectangle x:Name="MoveRect" Fill="White" Opacity="0.3" Stroke="Red" Width="30" Height="30" Canvas.Top="0" Canvas.Left="0"/>
        </Canvas>

 

        2、截圖框的移動spa

 private void MoveRect_MouseMove(object sender, MouseEventArgs e)
        {
            // 鼠標按下時才移動
            if (e.LeftButton == MouseButtonState.Released) return;

            FrameworkElement element = sender as FrameworkElement;

            //計算鼠標在X軸的移動距離
            double deltaV = e.GetPosition(MoveRect).Y - MoveRect.Height / 2;
            //計算鼠標在Y軸的移動距離
            double deltaH = e.GetPosition(MoveRect).X - MoveRect.Width / 2; ;
            //獲得圖片Top新位置
            double newTop = deltaV + (double)MoveRect.GetValue(Canvas.TopProperty);
            //獲得圖片Left新位置
            double newLeft = deltaH + (double)MoveRect.GetValue(Canvas.LeftProperty);

            //邊界的判斷
            if (newLeft <= 0)
            {
                newLeft = 0;
            }

            //左側圖片框寬度 - 半透明矩形框寬度
            if (newLeft >= (this.SmallBox.Width - this.MoveRect.Width))
            {
                newLeft = this.SmallBox.Width - this.MoveRect.Width;
            }

            if (newTop <= 0)
            {
                newTop = 0;
            }

            //左側圖片框高度度 - 半透明矩形框高度度
            if (newTop >= this.SmallBox.Height - this.MoveRect.Height)
            {
                newTop = this.SmallBox.Height - this.MoveRect.Height;
            }
            MoveRect.SetValue(Canvas.TopProperty, newTop);
            MoveRect.SetValue(Canvas.LeftProperty, newLeft);

        }

 

         3、圖片放大orm

          Timer事件刷新圖片,30次/秒,肉眼看上去就是視頻的效果啦
視頻

private void ImageTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                if (videoPlayer.VideoSource != null)
                {
                    bigImg.Source = videoPlayer.VideoSource;
                    imageBrush.ImageSource = videoPlayer.VideoSource;

                    #region
                    //獲取右側大圖框與透明矩形框的尺寸比率
                    double n = this.BigBox.Width / this.MoveRect.Width;

                    //獲取半透明矩形框在左側小圖中的位置
                    double left = (double)this.MoveRect.GetValue(Canvas.LeftProperty);
                    double top = (double)this.MoveRect.GetValue(Canvas.TopProperty);

                    //計算和設置原圖在右側大圖框中的Canvas.Left 和 Canvas.Top
                    bigImg.SetValue(Canvas.LeftProperty, -left * n);
                    bigImg.SetValue(Canvas.TopProperty, -top * n);
                    #endregion
                }
            }));
        }

 

完整的代碼:xml

(佈局)blog

<UserControl x:Class="QACDR.UI.VlcWpf"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:QACDR.UI"
             xmlns:vlc="clr-namespace:xZune.Vlc.Wpf;assembly=xZune.Vlc.Wpf"
             
             mc:Ignorable="d" Height="349.212" Width="620.7">
    <UserControl.Resources>
        <!--筆刷-->
        <LinearGradientBrush x:Key="SliderBackground"  StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0" Color="#59ccfc"/>
            <GradientStop Offset="0.5" Color="#00b3fe"/>
            <GradientStop Offset="1" Color="#FFFB1005"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="SliderThumb"  StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0" Color="#FFD9D3E8"/>
            <!--<GradientStop Offset="1" Color="#dfdfdf"/>-->
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="SliderText"  StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0" Color="#7cce45"/>
            <GradientStop Offset="1" Color="#4ea017"/>
        </LinearGradientBrush>

        <!--Slider模板-->
        <Style x:Key="Slider_RepeatButton" TargetType="RepeatButton">
            <Setter Property="Focusable" Value="false" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RepeatButton">
                        <Border Background="{StaticResource SliderBackground}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="Slider_RepeatButton1" TargetType="RepeatButton">
            <Setter Property="Focusable" Value="false" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RepeatButton">
                        <Border Background="Transparent" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="Slider_Thumb" TargetType="Thumb">
            <Setter Property="Focusable" Value="false" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Thumb">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Border Background="{StaticResource SliderBackground}"/>
                            <Border Grid.ColumnSpan="2" CornerRadius="4"  Background="{StaticResource SliderThumb}" Width="15">
                                <!--<TextBlock Text="||" HorizontalAlignment="Center" VerticalAlignment="Center"/>-->
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="Slider_CustomStyle" TargetType="Slider">
            <Setter Property="Focusable" Value="false" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Slider">
                        <Grid>
                            <!--<Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="40"/>
                            </Grid.ColumnDefinitions>-->
                            <Grid.Effect>
                                <DropShadowEffect BlurRadius="10" ShadowDepth="1" />
                            </Grid.Effect>
                            <!--<Border HorizontalAlignment="Right" BorderBrush="Gray" BorderThickness="1,1,0,1" Background="{StaticResource SliderText}" Width="80" CornerRadius="8,0,0,8"/>-->
                            <!--<Border Grid.Column="2" HorizontalAlignment="Right" BorderBrush="Gray" BorderThickness="0,1,1,1" Background="{StaticResource SliderText}" Width="40" CornerRadius="0,8,8,0"/>-->
                            <!--<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Tag}" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14"/>-->
                            <!--<TextBlock Grid.Column="2" Text="{Binding ElementName=PART_Track,Path=Value,StringFormat=\{0:N0\}}" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14" DataContext="{Binding}" />-->
                            <Border Grid.Column="1" BorderBrush="Gray" BorderThickness="1" CornerRadius="8,0,0,8">
                                <Track Grid.Column="1" Name="PART_Track">
                                    <Track.DecreaseRepeatButton>
                                        <RepeatButton Style="{StaticResource Slider_RepeatButton}"
                                Command="Slider.DecreaseLarge"/>
                                    </Track.DecreaseRepeatButton>
                                    <Track.IncreaseRepeatButton>
                                        <RepeatButton Style="{StaticResource Slider_RepeatButton1}"
                                Command="Slider.IncreaseLarge"/>
                                    </Track.IncreaseRepeatButton>
                                    <Track.Thumb>
                                        <Thumb Style="{StaticResource Slider_Thumb}"/>
                                    </Track.Thumb>
                                </Track>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid Name="girdParent" Background="#FF0A0909">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="32"/>
        </Grid.RowDefinitions>
        
        <vlc:VlcPlayer x:Name="videoPlayer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Visibility="Hidden"/>

        <DockPanel x:Name="AudioCmdPanel" DockPanel.Dock="Top" VerticalAlignment="Top" Height="20" Background="AliceBlue"  Grid.Row="1">
            <!--<Label Content="進度" VerticalAlignment="Center" DockPanel.Dock="Left"/>-->
            <Slider Margin="5,0,0,0" x:Name="videoSlider" Height="15" DockPanel.Dock="Top" Value ="{Binding Position, ElementName=videoPlayer}" Maximum="1" SmallChange="0.001" TickFrequency="100" LargeChange="0.1"  Style="{DynamicResource Slider_CustomStyle}" />
        </DockPanel>

        <DockPanel x:Name="AudioBtnPanel" DockPanel.Dock="Bottom" HorizontalAlignment="Center" Grid.Row="2">
            <!--<ComboBox Name="cbvChanel" DockPanel.Dock="Right" SelectionChanged="ComboBox_SelectionChanged" Margin="5,0,0,0" Visibility="Visible">
                <ComboBoxItem  IsSelected="True">立體聲</ComboBoxItem>
                <ComboBoxItem>左聲道</ComboBoxItem>
                <ComboBoxItem>右聲道</ComboBoxItem>
            </ComboBox>-->
            <!--<Label x:Name="lpSpeed" Content="播放速度:正常" HorizontalAlignment="Left" Margin="10,5,10,0" Grid.Row="2" VerticalAlignment="Top" Height="30" Width="110" Background="#FF0D0E0D" Foreground="#FF45EC14"/>
            <Label x:Name="lbTitle" Content="" HorizontalAlignment="Left" Margin="10,5,0,0" Grid.Row="2" VerticalAlignment="Top" Height="30" Width="120" Background="#FF0D0E0D" Foreground="#FF45EC14"/>-->

            <Button x:Name="btnStop" HorizontalAlignment="Center"  Command="{Binding VideoBackCommand}" Height="25" Width="25" Click="btnStop_Click" >
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border>
                            <Image x:Name="img" Source="pack://application:,,,/Image/stop.png"/>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button x:Name="VideoBack" HorizontalAlignment="Center"  Command="{Binding VideoBackCommand}" Height="25" Width="25" Click="VideoBack_Click">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border>
                            <Image x:Name="img" Source="pack://application:,,,/Image/BackOff.png"/>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button x:Name="VideoPlay" Margin="5,0,5,0" Command="{Binding VideoPlayCommand}" Height="30" Width="30" Click="VideoPlay_Click">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="btnBoder">
                            <Image x:Name="img" Source="pack://application:,,,/Image/Pause.png"/>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button  Command="{Binding VideoForwardCommand}" Height="25" Width="25" Click="Button_Click">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border>
                            <Image x:Name="img" Source="pack://application:,,,/Image/Forward.png"/>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>

            <!--<Label x:Name="lbTitle" Content="" DockPanel.Dock="Right" Margin="1,7,0,0"  Height="30" Width="120" Background="#FF0D0E0D" Foreground="#FF45EC14"/>
            <Label x:Name="lpSpeed" Content="播放速度:正常" DockPanel.Dock="Right" Margin="10,7,0,0" Height="30" Width="110" Background="#FF0D0E0D" Foreground="#FF45EC14"/>-->

            <CheckBox x:Name="cbSync" Content="同步音頻" Foreground="#FF45EC14" HorizontalAlignment="Right" Margin="10,10,0,0"  DockPanel.Dock="Right" Height="20" Width="66" Checked="cbSync_Checked" Unchecked="cbSync_Unchecked"/>
            <Slider x:Name="voiceSlider" HorizontalAlignment="Right" Margin="0,0,0,0" Height="10" Value="{Binding Volume, ElementName=videoPlayer}" DockPanel.Dock="Right" Width="80" Maximum="100"  Style="{DynamicResource Slider_CustomStyle}"/>

            <Label x:Name="lbVoice"  VerticalAlignment="Center" HorizontalAlignment="Right" Margin="5,0,0,0" DockPanel.Dock="Right" Height="30" Width="30" MouseDown="Label_MouseDown">
                <Image Source="pack://application:,,,/Image/voice.png"/>
            </Label>
        </DockPanel>

        <!--右側大圖-->
        <Canvas x:Name="BigBox" Width="643" Height="643" Canvas.Left="360" Canvas.Top="20" Visibility="Visible">
            <!--右側原圖片 注意尺寸-->
            <Image x:Name="bigImg" Width="2592" Height="1922" Canvas.Left="0" Canvas.Top="-780" />
            <Canvas.Clip>
                <RectangleGeometry Rect="0,0,643,643" />
            </Canvas.Clip>
        </Canvas>


        <!--左側小圖-->
        <Canvas x:Name="SmallBox" HorizontalAlignment="Left" VerticalAlignment="Top" MouseMove="MoveRect_MouseMove" Width="121" Height="90" Canvas.Left="20" Canvas.Top="20">
            <Canvas.Background>
                <ImageBrush Stretch="UniformToFill" />
            </Canvas.Background>
            <!--半透明矩形框-->
            <Rectangle x:Name="MoveRect" Fill="White" Opacity="0.3" Stroke="Red" Width="30" Height="30" Canvas.Top="0" Canvas.Left="0"/>
        </Canvas>
    </Grid>
</UserControl>

 (功能代碼)

using DevExpress.XtraEditors;
using NAudio.CoreAudioApi;
using QACDR.Common;
using QACDR.Common.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using xZune.Vlc.Wpf;

namespace QACDR.UI
{
    /// <summary>
    /// VlcWpf.xaml 的交互邏輯
    /// </summary>
    public partial class VlcWpf : UserControl
    {
        /// <summary>
        /// VLC參數
        /// </summary>
        private List<string> baseVlcSettings;
        /// <summary>
        /// vlclib路徑
        /// </summary>
        private string libVlcPath = "libvlc";
        ///音頻設備            
        private MMDevice audioDevice;
        /// <summary>
        /// 快進等級 1/2/3
        /// </summary>
        private int rateLevel = 1;
        /// <summary>
        /// 當前音量
        /// </summary>
        private int currVolume = 0;
        /// <summary>
        /// 是否靜音
        /// </summary>
        private bool bNoVoice = false;
        /// <summary>
        /// 視頻文件列表
        /// </summary>
        private List<VideoFileInfo> videoList = new List<VideoFileInfo>();
        /// <summary>
        /// 當前播放序號
        /// </summary>
        private int currIndex = 0;
        /// <summary>
        /// 圖片質量
        /// </summary>
        private int quality = 10;

        private System.Timers.Timer imageTimer = null;
        private ImageBrush imageBrush = new ImageBrush();

        public VlcWpf()
        {
            InitializeComponent();

            InitVlc();   // 初始化VLC

            EventPublisher.ChangeVolChEvent += EventPublisher_ChangeVolChEvent;
            EventPublisher.SearchDataEvent += EventPublisher_SearchDataEvent;
            videoPlayer.StateChanged += VideoPlayer_StateChanged;
            EventPublisher.ZoomVideoEvent += EventPublisher_ZoomVideoEvent;

            int interValue = 100;
            string str = ConfigurationManager.AppSettings["FreamCount"];
            if (!string.IsNullOrEmpty(str))
            {
                interValue = Convert.ToInt32(str);
            }

            string strQuality = ConfigurationManager.AppSettings["FreamCount"];
            if (!string.IsNullOrEmpty(strQuality))
                quality = Convert.ToInt32(strQuality);

            imageTimer = new System.Timers.Timer();
            imageTimer.Enabled = true;
            imageTimer.Interval = 30;
            imageTimer.Elapsed += ImageTimer_Elapsed;
            // 初始化時複製,避免內存增長
            SmallBox.Background = imageBrush;

            SmallBox.Visibility = Visibility.Hidden;
            MoveRect.Visibility = Visibility.Hidden;
            videoPlayer.Visibility = Visibility.Visible;
            bigImg.Visibility = Visibility.Hidden;

        }

        private void EventPublisher_ZoomVideoEvent(object sender, ZoomVideoEventArgs e)
        {
            if (e.IsZoom)
            {
                imageTimer.Start();
                SmallBox.Visibility = Visibility.Visible;
                MoveRect.Visibility = Visibility.Visible;
                BigBox.Visibility = Visibility.Visible;
                videoPlayer.Visibility = Visibility.Hidden;
                bigImg.Visibility = Visibility.Visible;
            }
            else
            {
                imageTimer.Stop();
                SmallBox.Visibility = Visibility.Hidden;
                MoveRect.Visibility = Visibility.Hidden;
                BigBox.Visibility = Visibility.Hidden;
                videoPlayer.Visibility = Visibility.Visible;
                bigImg.Visibility = Visibility.Hidden;
            }
        }

        private void ImageTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                if (videoPlayer.VideoSource != null)
                {
                    bigImg.Source = videoPlayer.VideoSource;
                    imageBrush.ImageSource = videoPlayer.VideoSource;

                    #region
                    //獲取右側大圖框與透明矩形框的尺寸比率
                    double n = this.BigBox.Width / this.MoveRect.Width;

                    //獲取半透明矩形框在左側小圖中的位置
                    double left = (double)this.MoveRect.GetValue(Canvas.LeftProperty);
                    double top = (double)this.MoveRect.GetValue(Canvas.TopProperty);

                    //計算和設置原圖在右側大圖框中的Canvas.Left 和 Canvas.Top
                    bigImg.SetValue(Canvas.LeftProperty, -left * n);
                    bigImg.SetValue(Canvas.TopProperty, -top * n);
                    #endregion
                }
            }));
        }

        /// <summary>
        /// 初始化VLC
        /// </summary>
        private void InitVlc()
        {
            try
            {
                baseVlcSettings = new List<string> { "-I", "dummy", "--avi-index=1", "--ignore-config", "--no-video-title", "--aout=directsound" };

                // 初始化:設置音頻輸出的默認通道
                bool bIsVlcInit = false;
                MMDeviceEnumerator dve = new MMDeviceEnumerator();
                MMDeviceCollection devices = dve.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
                foreach (var device in devices)
                {
                    if (device.ID == Properties.Settings.Default.AudioDeviceID)
                    {
                        audioDevice = device;
                        List<string> vlcSettings = new List<string>(baseVlcSettings);
                        string[] temp_params = audioDevice.ID.Split('.');
                        vlcSettings.Add(string.Format("--directx-audio-device={0}", temp_params[temp_params.Length - 1]));
                        this.videoPlayer.Initialize(libVlcPath, vlcSettings.ToArray());

                        bIsVlcInit = true;
                        break;
                    }
                }

                if (bIsVlcInit == false)
                {
                    this.videoPlayer.Initialize(libVlcPath, baseVlcSettings.ToArray());
                }

                currVolume = 100;
                videoPlayer.Volume = 100;
                voiceSlider.Value = 100;
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        /// <summary>
        /// 播放視頻
        /// </summary>
        /// <param name="filePath"></param>
        public void PlayVideo(VideoFileInfo videoInfo)
        {
            try
            {
                if (!File.Exists(videoInfo.path))
                {
                    MessageBox.Show("視頻文件不存在!");
                    return;
                }

                videoPlayer.BeginStop(new Action(delegate
                {
                    while (videoPlayer.VlcMediaPlayer.Media != null)
                    {
                        videoPlayer.VlcMediaPlayer.Media = null;
                        Thread.Sleep(200);
                    } // 添加此段代碼,修復連續播放視頻時會死掉的現象

                    if (videoPlayer.State == xZune.Vlc.Interop.Media.MediaState.Stopped || videoPlayer.State == xZune.Vlc.Interop.Media.MediaState.NothingSpecial)
                    {
                        videoPlayer.LoadMedia(videoInfo.path);
                        videoPlayer.Play();
                    }
                }));

                // 獲取當前播放的序號
                for (int i = 0; i < videoList.Count; i++)
                {
                    if (videoList[i].path == videoInfo.path && videoList[i].id == videoInfo.id)
                    {
                        currIndex = i;
                        break;
                    }
                }

                FileInfo fi = new FileInfo(videoInfo.path);
                this.Dispatcher.Invoke(new Action(delegate
                {
                    //lbTitle.Content = fi.Name;
                }));
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        /// <summary>
        /// 設置VLC音頻輸出端口
        /// </summary>
        /// <param name="options"></param>
        public void SelectAudioDevice(string[] options)
        {
            try
            {
                videoPlayer.BeginStop(new Action(delegate
                {
                    this.videoPlayer.Initialize(libVlcPath, options);
                }));
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        /// <summary>
        /// 視頻截圖
        /// </summary>
        public void Snapshot()
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "snapshot";
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                string name = path + "\\" + DateTime.Now.ToFileTime() + ".png";
                videoPlayer.TakeSnapshot(name, SnapshotFormat.JPG, 30);
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        #region 控件事件

        private void VideoPlayer_StateChanged(object sender, xZune.Vlc.ObjectEventArgs<xZune.Vlc.Interop.Media.MediaState> e)
        {
            if (e.Value == xZune.Vlc.Interop.Media.MediaState.Ended)
            {
                if (currIndex + 1 < videoList.Count)
                {
                    // 播放下一視頻
                    currIndex++;
                    PlayVideo(videoList[currIndex]);
                }
                else
                {
                    this.Dispatcher.Invoke(new Action(delegate
                    {
                        //lbTitle.Content = "播放完成。";
                    }));
                }

                //EventPublisher.PictureShowEvent(this, new PictureShowEventArgs() { StateShow = ShowEnum.Stop });
            }
            else if (e.Value == xZune.Vlc.Interop.Media.MediaState.Paused)
            {
            }
        }

        // 聲道切換事件
        private void EventPublisher_ChangeVolChEvent(object sender, ChangeVolChEventArgs e)
        {
            if (e.CType != 2) return;  // 若是不是音頻所發起的,則不執行
            //cbvChanel.Text = e.VolChStr;
        }

        // 查詢數據
        private void EventPublisher_SearchDataEvent(object sender, SearchDataEventArgs e)
        {
            string cmdText = "select * from QACDR_VIDEO where operation_id > 0 " + e.Condition + " order by start_time asc";
            DataSet ds = SQLiteHelper.ExecuteDataSet(cmdText);
            if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0) return;

            videoList.Clear();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                VideoFileInfo video = new VideoFileInfo();
                video.id = Convert.ToInt32(row["id"]);
                video.operation_id = Convert.ToInt32(row["operation_id"]);
                video.machine_id = Convert.ToInt32(row["machine_id"]);
                video.path = row["path"].ToString();
                video.start_time = Convert.ToDateTime(row["start_time"]);
                video.import_time = Convert.ToDateTime(row["import_time"]);
                videoList.Add(video);
            }
        }

        // 聲道
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //string str = cbvChanel.Text;
                //EventPublisher.PublishChangeVolChEvent(this, str, 1);
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        // 靜音/取消靜音
        private void Label_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (bNoVoice == true)
                {
                    currVolume = videoPlayer.Volume;
                    videoPlayer.Volume = 0;
                    System.Windows.Controls.Image img = new Image();
                    img.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"Image\NoVoice.png"));
                    lbVoice.Content = img;
                    voiceSlider.IsEnabled = false;
                    bNoVoice = false;
                }
                else
                {
                    videoPlayer.Volume = currVolume;
                    System.Windows.Controls.Image img = new Image();
                    img.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"Image\Voice.png"));
                    lbVoice.Content = img;
                    voiceSlider.IsEnabled = true;
                    bNoVoice = true;
                }
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        // 中止
        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                videoPlayer.BeginStop(new Action(delegate
                {
                    this.Dispatcher.Invoke(new Action(delegate
                    {
                        //lbTitle.Content = "播放完成。";
                    }));
                }));
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        // 快退
        private void VideoBack_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                float tp = videoPlayer.Position - (float)0.05;
                if (tp > 0)
                {
                    videoPlayer.Position = tp;
                }
                else
                {
                    videoPlayer.Position = 0;
                }
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        // 播放/暫停
        private void VideoPlay_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (videoPlayer.State == xZune.Vlc.Interop.Media.MediaState.Playing)
                {
                    System.Windows.Controls.Border boder = (System.Windows.Controls.Border)VideoPlay.Template.FindName("btnBoder", VideoPlay);
                    System.Windows.Controls.Image img = (System.Windows.Controls.Image)boder.FindName("img");
                    img.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"Image\Play.png"));
                }
                else if (videoPlayer.State == xZune.Vlc.Interop.Media.MediaState.Paused)
                {
                    System.Windows.Controls.Border boder = (System.Windows.Controls.Border)VideoPlay.Template.FindName("btnBoder", VideoPlay);
                    System.Windows.Controls.Image img = (System.Windows.Controls.Image)boder.FindName("img");
                    img.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"Image\Pause.png"));
                }

                videoPlayer.PauseOrResume();
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        // 快進
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (rateLevel == 1)
                {
                    rateLevel = 2;
                    videoPlayer.Rate = 3.0f;
                    //lpSpeed.Content = "播放速度:快進x1";
                }
                else if (rateLevel == 2)
                {
                    rateLevel = 3;
                    videoPlayer.Rate = 5.0f;
                    //lpSpeed.Content = "播放速度:快進x2";
                }
                else if (rateLevel == 3)
                {
                    rateLevel = 4;
                    videoPlayer.Rate = 7.0f;
                    //lpSpeed.Content = "播放速度:快進x3";
                }
                else if (rateLevel == 4)
                {
                    rateLevel = 1;
                    videoPlayer.Rate = 1;
                    //lpSpeed.Content = "播放速度:正常";
                }
            }
            catch (Exception ex)
            {
                Log4Allen.WriteLog(typeof(VlcWpf), ex);
            }
        }

        // 同步音頻
        private void cbSync_Checked(object sender, RoutedEventArgs e)
        {
            if (Utils.Mode == SyncModeEnum.DDS)
            {
                XtraMessageBox.Show("正在進行DDS同步,視頻同步音頻。");
                return;
            }

            EventPublisher.PublishSetAudioSyncEvent(this, new SetAudioSyncEventArgs() { IsSync = true });
            Utils.Mode = SyncModeEnum.VIDEO;
        }

        private void cbSync_Unchecked(object sender, RoutedEventArgs e)
        {
            EventPublisher.PublishSetAudioSyncEvent(this, new SetAudioSyncEventArgs() { IsSync = false });
            Utils.Mode = SyncModeEnum.NONE;
        }
        #endregion

        private void MoveRect_MouseMove(object sender, MouseEventArgs e)
        {
            // 鼠標按下時才移動
            if (e.LeftButton == MouseButtonState.Released) return;

            FrameworkElement element = sender as FrameworkElement;

            //計算鼠標在X軸的移動距離
            double deltaV = e.GetPosition(MoveRect).Y - MoveRect.Height / 2;
            //計算鼠標在Y軸的移動距離
            double deltaH = e.GetPosition(MoveRect).X - MoveRect.Width / 2; ;
            //獲得圖片Top新位置
            double newTop = deltaV + (double)MoveRect.GetValue(Canvas.TopProperty);
            //獲得圖片Left新位置
            double newLeft = deltaH + (double)MoveRect.GetValue(Canvas.LeftProperty);

            //邊界的判斷
            if (newLeft <= 0)
            {
                newLeft = 0;
            }

            //左側圖片框寬度 - 半透明矩形框寬度
            if (newLeft >= (this.SmallBox.Width - this.MoveRect.Width))
            {
                newLeft = this.SmallBox.Width - this.MoveRect.Width;
            }

            if (newTop <= 0)
            {
                newTop = 0;
            }

            //左側圖片框高度度 - 半透明矩形框高度度
            if (newTop >= this.SmallBox.Height - this.MoveRect.Height)
            {
                newTop = this.SmallBox.Height - this.MoveRect.Height;
            }
            MoveRect.SetValue(Canvas.TopProperty, newTop);
            MoveRect.SetValue(Canvas.LeftProperty, newLeft);

        }

        public void ResizeCtrl()
        {
        }
    }
}
相關文章
相關標籤/搜索