I'm trying to figure out how to get the "View size" of an image which is set as the background on a Canvas
control.
The background is set with a ImageBrush
, with strech set to UniForm.
If the strech property was set to Fill, then I could just use the ActualWidth and ActualHeight of the canvas.
This is not possible with UniForm, so how should I approach this?
Here is my XAML:
<Grid DataContext="{Binding ImageViewerVM.CurrentImage}">
<controls:RubberBandingCanvas x:Name="RubCanvas">
<controls:RubberBandingCanvas.Resources>
<Converters:ControlVisibilityConverter x:Key="VisibilityHiddenConverter" />
<Converters:StringToBitmapConverter x:Key="StringToBitmapConverter" />
</controls:RubberBandingCanvas.Resources>
<controls:RubberBandingCanvas.Background>
<ImageBrush x:Name="ibCurrentImage" ImageSource="{Binding Path=Path, Converter={StaticResource StringToBitmapConverter}}" Stretch="Uniform"></ImageBrush>
</controls:RubberBandingCanvas.Background>
<Ellipse Canvas.Top="10" Canvas.Right="10" Fill="Red" Stroke="Black" Height="15" Width="15" Visibility="{Binding Path=IsMultiTiff, Converter={StaticResource VisibilityHiddenConverter}}"/>
</controls:RubberBandingCanvas>
</Grid>
Best regards,
Jesper Jensen
There is no way to get the actual size of the ImageBrush out of the box. What I would do would be to compute manually this size based on the Canvas RenderSize.
var ratio = Math.Min(RubCanvas.RenderSize.Width / CanvasBackground.ImageSource.Width, RubCanvas.RenderSize.Height / CanvasBackground.ImageSource.Height);
var imageBrushWidth = CanvasBackground.ImageSource.Width * ratio;
var imageBrushHeight = CanvasBackground.ImageSource.Height * ratio;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With