图片循环播放器。WPF制作 以下为代码

原创 2012年03月22日 17:43:30
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media.Animation;
using System.Threading;


namespace QinQinGo.MyControl
{
    /// <summary>
    /// AdvertPicControl.xaml 的交互逻辑 广告图片控件 播放完毕后自我销毁
    /// </summary>
    public partial class AdvertPicControl : UserControl
    {
        #region 加载List数据
        /// <summary>
        /// 当前图片地址播放列表
        /// </summary>
        private static List<string> currentList;

        public static DependencyProperty advertPicList = DependencyProperty.Register("advertPicList", typeof(List<string>), typeof(AdvertPicControl)
            , new PropertyMetadata(new PropertyChangedCallback(loadAdvertPic)));

        public List<string> AdvertPicList
        {
            get { return (List<string>)GetValue(advertPicList); }
            set { SetValue(advertPicList, value); }
        }

        /// <summary>
        /// 图片播放器地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void loadAdvertPic(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            AdvertPicControl advertPicControl = (AdvertPicControl)sender;
            if (e.Property == advertPicList)
            {
                advertPicControl.AdvertPicList = (List<string>)e.NewValue;
                currentList = advertPicControl.AdvertPicList;
            }
        }
        #endregion

        #region 加载图片停留时间
        /// <summary>
        /// 当前图片地址播放列表
        /// </summary>
        private static List<int> currentTimeList;

        public static DependencyProperty advertPicStayTime = DependencyProperty.Register("advertPicStayTime", typeof(List<int>), typeof(AdvertPicControl)
            , new PropertyMetadata(new PropertyChangedCallback(loadAdvertStayTime)));

        public List<int> AdvertPicStayTime
        {
            get { return (List<int>)GetValue(advertPicStayTime); }
            set { SetValue(advertPicStayTime, value); }
        }

        /// <summary>
        /// 图片播放器图片停留时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void loadAdvertStayTime(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            AdvertPicControl advertPicControl = (AdvertPicControl)sender;
            if (e.Property == advertPicStayTime)
            {
                advertPicControl.AdvertPicStayTime = (List<int>)e.NewValue;
                currentTimeList = advertPicControl.AdvertPicStayTime;
            }
        }
        #endregion

        #region 注册自定义事件和参数
        public static readonly RoutedEvent AdvertPicPlayStateChangedEvent;

        public class AdvertPicPlayEventArgs : RoutedEventArgs
        {
            public int playState
            {
                get;
                set;
            }

            public int playLength
            {
                get;
                set;
            }

            public int playIndex
            {
                get;
                set;
            }
        }

        static AdvertPicControl()
        {
            AdvertPicPlayStateChangedEvent = EventManager.RegisterRoutedEvent("AdvertPicPlayStateChanged",
                RoutingStrategy.Bubble, typeof(AdvertPicPlayStateChangedHandler), typeof(AdvertPicControl));
        }
        public delegate void AdvertPicPlayStateChangedHandler(object sender, AdvertPicPlayEventArgs e);
        public event AdvertPicPlayStateChangedHandler AdvertPicPlayStateChanged
        {
            add { AddHandler(AdvertPicControl.AdvertPicPlayStateChangedEvent, value); }
            remove { RemoveHandler(AdvertPicControl.AdvertPicPlayStateChangedEvent, value); }
        }
        #endregion


        public AdvertPicControl()
        {
            InitializeComponent();
        }

        DispatcherTimer switchPicTimer = new DispatcherTimer();
        int i = 0;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //默认 1秒切换一张图片
            switchPicTimer.IsEnabled = false;
            switchPicTimer.Tick += SwitchPicEvent;
        }

        /// <summary>
        /// 开始播放
        /// </summary>
        /// <param name="interval">图片切换时间</param>
        public void Play(int interval)
        {
            switchPicTimer.IsEnabled = true;
            switchPicTimer.Interval = new TimeSpan(0, 0, 1);
            switchPicTimer.Start();
            i = 0;
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            switchPicTimer.IsEnabled = false;
            switchPicTimer.Stop();
        }

        /// <summary>
        /// 切换图片事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SwitchPicEvent(object sender, EventArgs e)
        {
            if (null != currentList)
            {
                Console.WriteLine("开始切换~~~");
                if (i < currentList.Count - 1)
                {
                    DoHandlerStop(Image.OpacityProperty, 1, 0, 1, imgAdvertPic, SwitchPic);
                }
                else
                {
                    AdvertPicPlayEventArgs args = new AdvertPicPlayEventArgs();
                    args.RoutedEvent = AdvertPicPlayStateChangedEvent;
                    args.playState = 1;
                    RaiseEvent(args);
                    switchPicTimer.Stop();
                    switchPicTimer.IsEnabled = false;
                }
                if (null != currentTimeList)
                {
                    Thread.Sleep(currentTimeList[i]); //图片停留时间
                }
            }
        }

        /// <summary>
        /// 动画播放完毕切换图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SwitchPic(object sender, EventArgs e)
        {
            BitmapImage bitmap = new BitmapImage(new Uri(currentList[i], UriKind.Absolute));
            imgAdvertPic.Stretch = Stretch.Fill;
            imgAdvertPic.Source = bitmap;
            if (i < currentList.Count)
            {
                i++;
            }
        }

        /// <summary>
        /// 动画
        /// </summary>
        /// <param name="dp"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="duration"></param>
        /// <param name="element"></param>
        /// <param name="complateHander"></param>
        public void DoHandlerStop(DependencyProperty dp, double from, double to, double duration, UIElement element, EventHandler complateHander)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();//创建双精度动画对象
            doubleAnimation.From = from;
            doubleAnimation.To = to;//设置动画的结束值
            doubleAnimation.Duration = TimeSpan.FromSeconds(duration);//设置动画时间线长度
            doubleAnimation.FillBehavior = FillBehavior.Stop;//设置动画完成后执行的操作 
            doubleAnimation.Completed += complateHander;
            element.BeginAnimation(dp, doubleAnimation);//设置动画应用的属性并启动动画
        }
    }
}

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.Navigation;
using System.Windows.Shapes;
using System.IO;
using QinQinGo.MyControl;

namespace PicSwitcher
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取当前用户的图片文件夹中的图片(不包含子文件夹)
        /// </summary>
        /// <returns>返回图片路径列表</returns>
        private List<string> GetUserImages()
        {
            List<string> images = new List<string>();
            string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] files = GetPicFiles(path, "*.jpg,*.png,*.bmp,*.gif,", SearchOption.TopDirectoryOnly);// dir.GetFiles("*.jpg", SearchOption.AllDirectories);

            if (files != null)
            {
                foreach (FileInfo file in files)
                {
                    images.Add(file.FullName);
                }
            }
            return images;
        }

        public FileInfo[] GetPicFiles(string picPath, string searchPattern, SearchOption searchOption)
        {
            System.Collections.Generic.List<FileInfo> ltList = new List<FileInfo>();
            DirectoryInfo dir = new DirectoryInfo(picPath);
            string[] sPattern = searchPattern.Replace(';', ',').Split(',');
            for (int i = 0; i < sPattern.Length; i++)
            {
                FileInfo[] files = null;
                try
                {
                    files = dir.GetFiles(sPattern[i], searchOption);
                }
                catch (System.Exception ex)
                {
                    files = new FileInfo[] { };
                }

                ltList.AddRange(files);
            }
            return ltList.ToArray();
        }
        AdvertPicControl advertPic = new AdvertPicControl();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<string> imageList = GetUserImages();
            advertPic.AdvertPicList = this.GetUserImages();
            grdContent.Children.Add(advertPic);
            advertPic.AdvertPicPlayStateChanged += playStateHandler;
        }


        private void playStateHandler(object sender, AdvertPicControl.AdvertPicPlayEventArgs args)
        {
            MessageBox.Show("播放完了,触发事件....");
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            advertPic.Play(Convert.ToInt32(tbTime.Text)); //设置默认切换时间
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            advertPic.Stop();
        }
    }
}

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放、图片立体轮播、图片倒影立体滚动)效果实现

去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用。客户端是Window7操作系统,54寸大屏电脑电视一体机。要求有很炫的展示效果,要有一定的视觉冲击力,可触控操作。当然满足客户的要求也可以有...

WPF 动态图片切换

利用WPF 状态切换效果可以在WinForm 中做出如网页Flash 效果 主要代码如下: WPF 部份 using System; using Microsoft.Expression.In...
  • A_A66
  • A_A66
  • 2011年08月08日 17:48
  • 8400

WPF中播放帧序列图

一般我们播放视频时是直接播放相应的视频文件,但有时候需要播放的素材并不是视频文件格式,而是对应的每一帧图片所组成的序列图,此时使用storyboard可以实现序列图的播放。...
  • openzpc
  • openzpc
  • 2015年08月16日 23:24
  • 1575

WPF打开操作Office文档的解决办法

最近在一个项目碰到一个问题,要用WPF打开并显示打印Office文档。包括Word, Excel, 和PowerPoint 文档。找了很多资料,解决方案之一:调用第三方开发的Word, Excel, ...
  • soft2buy
  • soft2buy
  • 2011年05月26日 20:42
  • 11157

类项目中的配置文件app.config在打包安装后的信息获取的问题

在一个项目中碰到这样的一个问题,做一个WORD插件,功能在类库项目中实现了,配置信息存在类库项目的配置文件app.config中,在进行打包后,获取的配置文件中的DocType节点信息时,使用以下方法...

用WPF控件MediaElement创建简易播放器(视频区域、播放控制区域、播放列表、循环播放)

软件概览: 区域组成:一个MediaElement控件+一个ListView控件+一个TextBlock控件+一个Slider控件+若干button控件 功能介绍: 1、...

Wav 无缝循环播放器及实现代码

  • 2017年01月16日 09:23
  • 6.31MB
  • 下载

自定义循环滚动的图片播放器 ScrollingImageListView说明及示例

ScrollingImageListView 的 Demo已分享在Github,源码注释清晰,欢迎查看指正。ScrollingImageListView说明及示例先看效果图ScrollingImage...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:图片循环播放器。WPF制作 以下为代码
举报原因:
原因补充:

(最多只允许输入30个字)