WPF 通过Image控件实现多张图片的播放

开发环境建议使用VS2010及以上

效果:

WPF 通过Image控件实现多张图片的播放_第1张图片

 

xaml代码:


    
        
    

C#代码(后端代码)

using System;
using System.Windows;
using System.ComponentModel;

namespace PictureTest
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public class GenerateRandomImagepath : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
             if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
        }

        private string _imageFullPath;

        Random random = new Random((int)DateTime.Now.Ticks);
        readonly int Minint;
        readonly int Maxint;
        readonly string PrefixImageName;
        readonly string ImageExtension;
        /// 
        /// Used in data Binding
        /// 
        public string ImageFullPath { get{return _imageFullPath;} 
                                    set
                                    {
                                     _imageFullPath = value;
                                     OnPropertyChanged("ImageFullPath");
                                    } 
                                  }

        public GenerateRandomImagepath(string prefixName, string extension)
        {
            this.PrefixImageName = prefixName;
            this.Minint = 1;
            this.Maxint = 6;
            this.ImageExtension = extension;
        }

        int RandomNumber()
        {
            return random.Next(this.Minint, this.Maxint);
        }

        public void GenerateNewRandomImagePath()
        {
            this.ImageFullPath = this.PrefixImageName + RandomNumber() + this.ImageExtension;
        }

    }

	public partial class MainWindow : Window
	{
        GenerateRandomImagepath RandomImagePath;
        System.Timers.Timer timer = new System.Timers.Timer(2000);
      
		public MainWindow()
		{
			this.InitializeComponent();

			// 在此点下面插入创建对象所需的代码。
            RandomImagePath = new GenerateRandomImagepath(@"C:\Users\Administrator\Documents\Expression\Blend 4\Projects\PictureTest\PictureTest\Image\", @".jpg");
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
            //timer.Start();

            this.DataContext = RandomImagePath;
            RandomImagePath.GenerateNewRandomImagePath();
		}
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            RandomImagePath.GenerateNewRandomImagePath();
        }
	}
}

 

转载于:https://my.oschina.net/u/2525682/blog/699941

你可能感兴趣的:(WPF 通过Image控件实现多张图片的播放)