There is a post exactly similar to this where the TS binds a Rect instead of Int32Rect to the SourceRect property of CroppedBitmap. My current rep doesn't allow me to comment to ask more question, so I post a new one. Is there any reason why the code below results in an image that is NOT cropped.
ViewModel
public class ViewModel
{
public Int32Rect SourceRect { set; get; }
public ViewModel()
{
this.SourceRect = new Int32Rect(5, 5, 5, 5);
}
}
XAML
<Grid Name="myGrid">
<Grid.Resources>
<BitmapImage x:Key="mainImage" UriSource="someImage.bmp"/>
</Grid.Resources>
<StackPanel>
<Image>
<Image.Source>
<CroppedBitmap Source="{StaticResource mainImage}" SourceRect="{Binding SourceRect}" />
</Image.Source>
</Image>
</StackPanel>
</Grid>
XAML.cs
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent();
ViewModel viewModel = new ViewModel();
this.DataContext = viewModel;
}
}
Update
This explains why this doesn't work as pointed out by NETscape.