wpf2016. 4. 8. 16:54

이미지 여러장을 이용해서 애니메이션을 만드는것


ImageSequence.zip


참고 : http://coderelief.net/2009/05/21/frame-based-animation-in-wpf/


public partial class MainWindow : Window
{
public static readonly DependencyProperty FramesPerSecondProperty = DependencyProperty
.Register("FramesPerSecond", typeof(double), typeof(MainWindow));
private List<string> imageList = new List<string>();
private TimeSpan LastRenderTime { get; set; }
public double FramesPerSecond
{
get { return (double)GetValue(FramesPerSecondProperty); }
set
{
if (value <= 0)
{
throw new Exception("FramesPerSecond must be greater than 0.");
}
SetValue(FramesPerSecondProperty, value);
}
}
private int index = 1;
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 21; i++)
{
imageList.Add(string.Format("/ImageSequence;component/Images/e2000{0}.png", i));
}
FramesPerSecond = 10;
CompositionTarget.Rendering += CompositionTarget_Rendering;
}
private void CompositionTarget_Rendering(object sender, EventArgs e)
{
TimeSpan timeSinceLastRender;
timeSinceLastRender = (DateTime.Now.TimeOfDay - LastRenderTime);
if (timeSinceLastRender.TotalSeconds < (1 / FramesPerSecond))
{
return;
}
LastRenderTime = DateTime.Now.TimeOfDay;
if (index < 20)
{
xImage.Source = new BitmapImage(new Uri(imageList[index], UriKind.RelativeOrAbsolute));
index++;
}
else
{
index = 1;
}
}
}


Posted by 동동(이재동)