Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Rectangle Width="100" Height="150">
<ImageBrush ImageSource="/Images/OwlSpritesheet.png"
<TranslateTransform x:Name="SpriteSheetOffset" X="0" Y="0" />
namespace SpottedZebra.Examples.WinRT
using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
public sealed partial class SpritesheetExamplePage : Page
private const int NumberOfColumns = 10;
private const int NumberOfFrames = 51;
private const int FrameWidth = 100;
private const int FrameHeight = 150;
public static readonly TimeSpan TimePerFrame = TimeSpan.FromSeconds(0.5);
private int currentFrame;
private TimeSpan timeTillNextFrame;
public SpritesheetExamplePage()
protected override void OnNavigatedTo(NavigationEventArgs e)
CompositionTarget.Rendering += this.OnUpdate;
protected override void OnNavigatedFrom(NavigationEventArgs e)
CompositionTarget.Rendering -= this.OnUpdate;
private void OnUpdate(object sender, object e)
this.timeTillNextFrame += TimeSpan.FromSeconds(1 / 60f);
if (this.timeTillNextFrame > TimePerFrame)
this.currentFrame = (this.currentFrame + 1 + NumberOfFrames) % NumberOfFrames;
var column = this.currentFrame % NumberOfColumns;
var row = this.currentFrame / NumberOfColumns;
this.SpriteSheetOffset.X = -column * FrameWidth;
this.SpriteSheetOffset.Y = -row * FrameHeight;
Copy link

ghost commented Aug 13, 2014

Thanks, I used this code. However I found timeTillNextFrame isn't set back to zero so the animation goes extremely fast. I added timeTillNextFrame = TimeSpan.Zero; after line 50 and now it plays at a constant regulated speed.

Copy link

JordanMachado commented Apr 23, 2015

Hi, I'm currently working on the microsoft surface and your example doesn't work. In fact my sprite is automaticaly croped .. Have you got an idea ?

Copy link

jameshez commented Sep 11, 2015

Also tested with Win10 UWP...blinking but without animation ...trying to figure out how to make it work

Copy link

HanBrouwer commented Oct 12, 2015

I tested it in VS 2015 Community with Windows 10. It workes fine. I added the same line as ssmninja. I set all the fields to private and changed the var's to int's. I also deleted all the this. prefixes.

Copy link

geoffles commented Aug 6, 2016

I seem to be having a similar problem to jameshez and JordanMachado - any ideas? I can confirm that my sprite is being cropped by something (Ie, only the 1 frame of the sprite seems to be loaded).

Copy link

geoffles commented Aug 6, 2016

Ok I've figured it out for anyone else who's struggling. You need to set the viewport of the imagebrush to the dimensions of your sprite - I presume WPF is cropping the image when loading it to optimise (if you are dynamically loading the image).

Copy link

schroding3rscat commented May 22, 2017

@geoffles, thank you. I struggled with this for a whole day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment