我正在试图弄清楚如何获取在Canvas
控件上设置为背景的图像的“视图大小”。
背景设置为ImageBrush
,strech设置为UniForm
如果strech属性设置为Fill,那么我可以使用画布的ActualWidth和ActualHeight。
这对UniForm来说是不可能的,所以我该如何处理呢?
这是我的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>
最好的问候,
Jesper Jensen
答案 0 :(得分:1)
无法获得开箱即用的ImageBrush的实际大小。我会做的是根据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;