5

我试图弄清楚如何获取设置为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>


最好的问候,
杰斯珀詹森

4

1 回答 1

2

没有办法让 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;
于 2012-11-09T10:00:38.747 回答