Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the actual size of an imagebrush control for a canvas background

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

like image 544
Jesper Jensen Avatar asked Nov 03 '22 12:11

Jesper Jensen


1 Answers

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;
like image 97
Sisyphe Avatar answered Nov 13 '22 21:11

Sisyphe