图片循环播放器。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();
        }
    }
}

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

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

WPF 图片自动播放

  • 2011年05月13日 08:47
  • 4.99MB
  • 下载

WPF循环显示图片解决OutofMemoryException方法

在做一个程序需要循环显示一个非常大的图片(2W)+(*2W+),使用的是image.Source=BitmapSource.Creat(),只要到这句代码,就蹦出来OutofMemoryExcepti...
  • zhounice1234
  • zhounice1234
  • 2017年09月06日 15:40
  • 246

WPF中播放帧序列图

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

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W...
  • wangrenzhu2011
  • wangrenzhu2011
  • 2012年03月22日 17:43
  • 2571

(精)N张图片在网页上循环播放,效果很是不错

 http://www.sina.com.cn --> bbs http://bbs.sina.com.cn -->要完成此效果需要两个步骤第一步:把如下代码加入到区域中-->STYLE>.........
  • W_Andy
  • W_Andy
  • 2007年12月04日 09:32
  • 2325

Unity3D循环滚动的背景图片?制作方法!!!!

链接: 真是抱歉,从今天起才认真的翻阅android game example 这个插件,开始熟悉一下外国佬的一些编程方式,相信很多人没有看过我就当做一个记录吧,都是一些简单的小东西。 首先就...
  • liqingmiao123
  • liqingmiao123
  • 2014年04月17日 17:54
  • 2257

用vb制作简单的播放器

看了这篇文章的标题,你是不是想拥有一个自己制作的播放器呢?那就继续向下看吧。       1、因为我们需要使用到MediaPlayer控件,所以刚开始就要右击工具面板并在部件添加窗口里选择“Wind...
  • renmengmeng520
  • renmengmeng520
  • 2016年03月22日 20:33
  • 2485

图片循环播放,可以控制时间

网上有很多实现图片循环播放的例子,我这里结合众多案例做了一个jar包 SwitchingView.jar下载地址http://download.csdn.net/detail/u013664211/8...
  • u013664211
  • u013664211
  • 2015年03月13日 18:10
  • 397

WPF 图片轮播

  • 2015年08月14日 09:46
  • 282KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:图片循环播放器。WPF制作 以下为代码
举报原因:
原因补充:

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