When I try to change the CroppedBitmap SourceRect property at runtime, nothing happens. There is no error, and the value of the property does not actually change.
I am trying to make sprite animations. I have a BitmapSource that contains a spritesheet, which is a single bitmap containing a grid of different poses for the sprite. Then I have CroppedBitmap, which has a spritesheet as its Source, and SourceRect, which pulls one of the poses from the sprite. At run time, when I want to animate, I try to change the CroppedBitmap SourceRect property to pull another pose from a larger bitmap; but, as noted above, the new property value simply does not stick. This is the strangest thing.
Here is an XAML example:
<UserControl.Resources> <BitmapImage x:Key="spritesheet" UriSource="Sprites/elf.png"/> </UserControl.Resources> <Image> <Image.Source> <CroppedBitmap x:Name="image" Source="{StaticResource spritesheet}" SourceRect="240 640 240 320"/> </Image.Source> </Image>
And codebehind is trying to do this:
var newRect = new Int32Rect(...); Debug.WriteLine(" Before: " + image.SourceRect); Debug.WriteLine("Assigning new value: " + newRect); image.SourceRect = newRect; Debug.WriteLine(" After: " + image.SourceRect);
This gives me this debug output:
Before: 240,640,240,320 Assigning new value: 240,0,240,320 After: 240,640,240,320
Therefore, it actually assigns a new rectangle (with Y = 0) to the property; no exception; but then the property value just didn't change (Y is still 640).
Any ideas on why this is happening and how to fix it?