Get actual brush image control size for canvas background

I am trying to figure out how to get the “View Size” of the image that is set as the background in the Canvas control. The background is set using ImageBrush , with strech installed in UniForm.
If the strech property was set to Fill, then I could just use ActualWidth and ActualHeight on the canvas.

This is not possible in 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> 


Yours faithfully,
Jesper Jensen

+4
source share
1 answer

Unable to get actual ImageBrush size out of the box. What I would do is manually calculate this size based on 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; 
+1
source

Source: https://habr.com/ru/post/1444291/


All Articles