您的位置:首页 > 其它

一个简单的WPF图片动画制作

2011-03-31 10:10 441 查看
制作一系列连续的图片,按顺序命名,如001.png,002.png....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using IO = System.IO;

namespace WpfImageAnimation
{
public partial class Window1 : Window
{
readonly IEnumerable<BitmapImage> _images;
IEnumerator<BitmapImage> _imageEnum;

public Window1()
{
InitializeComponent();

this.Loaded += this.Window1_Loaded;

string exe = Assembly.GetExecutingAssembly().Location;
string exeDir = IO.Path.GetDirectoryName(exe);
string imgDir = IO.Path.Combine(exeDir, "images");

_images =
from file in IO.Directory.GetFiles(imgDir, "*.png")
orderby file
let uri = new Uri(file, UriKind.Absolute)
select new BitmapImage(uri);
}

void Window1_Loaded(object sender, RoutedEventArgs e)
{
var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(180);
timer.Tick += this.timer_Tick;
timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
if (_imageEnum == null || !_imageEnum.MoveNext())
{
_imageEnum = _images.GetEnumerator();
_imageEnum.MoveNext();
}

this.image.Source = _imageEnum.Current;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: